/**
  ******************************************************************************
  * @file    sym32l010_hal_atim.h
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   Header file of ATIM HAL module.
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SYM32L010_HAL_ATIM_H
#define __SYM32L010_HAL_ATIM_H

#ifdef __cplusplus
extern "C" {
#endif


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "sym32l010_hal_def.h"

/******************************************************************************/
/* Global type definitions                                                    */
/******************************************************************************/

/* ATIM 计数模式 --------------------------------------------------------------*/
typedef enum
{
    ATIM_COUNTERMODE_UP                 = (0x0UL << ATIM_CR1_DIR_Pos),          // 递增计数模式
    ATIM_COUNTERMODE_DOWN               = (0x1UL << ATIM_CR1_DIR_Pos),          // 递减计数模式
    ATIM_COUNTERMODE_CENTERALIGNED1     = (0x1UL << ATIM_CR1_CMS_Pos),          // 中心对齐模式1
    ATIM_COUNTERMODE_CENTERALIGNED2     = (0x2UL << ATIM_CR1_CMS_Pos),          // 中心对齐模式2
    ATIM_COUNTERMODE_CENTERALIGNED3     = (0x3UL << ATIM_CR1_CMS_Pos)           // 中心对齐模式3
} ATIM_CountModeTypeDef;

/* ATIM 滤波时钟分频比 --------------------------------------------------------*/
typedef enum
{
    ATIM_CLOCKDIVISION_DIV1             = (0x0UL << ATIM_CR1_CKD_Pos),          // fDTS = fPCLK
    ATIM_CLOCKDIVISION_DIV2             = (0x1UL << ATIM_CR1_CKD_Pos),          // fDTS = fPCLK / 2
    ATIM_CLOCKDIVISION_DIV4             = (0x2UL << ATIM_CR1_CKD_Pos)           // fDTS = fPCLK / 4
} ATIM_ClockDivisionTypeDef;

/* ATIM 预装载使能控制 --------------------------------------------------------*/
typedef enum
{
    ATIM_AUTORELOAD_PRELOAD_DISABLE     = (0x0UL << ATIM_CR1_ARPE_Pos),         // 使能预装载
    ATIM_AUTORELOAD_PRELOAD_ENABLE      = (0x1UL << ATIM_CR1_ARPE_Pos)          // 禁止预装载
} ATIM_AutoReloadPreloadTypeDef;

/* ATIM 通道输出模式 ----------------------------------------------------------*/
typedef enum
{
    ATIM_OCMODE_TIMING                  = (0x0000UL << ATIM_CCMR1CMP_OC1M_Pos), // 冻结模式
    ATIM_OCMODE_ACTIVE                  = (0x0001UL << ATIM_CCMR1CMP_OC1M_Pos), // 匹配时置1
    ATIM_OCMODE_INACTIVE                = (0x0002UL << ATIM_CCMR1CMP_OC1M_Pos), // 匹配时置0
    ATIM_OCMODE_TOGGLE                  = (0x0003UL << ATIM_CCMR1CMP_OC1M_Pos), // 匹配时翻转
    ATIM_OCMODE_PWM1                    = (0x0006UL << ATIM_CCMR1CMP_OC1M_Pos), // PWM 模式1
    ATIM_OCMODE_PWM2                    = (0x0007UL << ATIM_CCMR1CMP_OC1M_Pos), // PWM 模式2
    ATIM_OCMODE_FORCED_ACTIVE           = (0x0005UL << ATIM_CCMR1CMP_OC1M_Pos), // 强制置1
    ATIM_OCMODE_FORCED_INACTIVE         = (0x0004UL << ATIM_CCMR1CMP_OC1M_Pos), // 强制清0
    ATIM_OCMODE_RETRIGERRABLE_OPM1      = (0x1000UL << ATIM_CCMR1CMP_OC1M_Pos), // 可再触发OPM1
    ATIM_OCMODE_RETRIGERRABLE_OPM2      = (0x1001UL << ATIM_CCMR1CMP_OC1M_Pos), // 可再触发OPM2
    ATIM_OCMODE_COMBINED_PWM1           = (0x1004UL << ATIM_CCMR1CMP_OC1M_Pos), // 组合 PWM 模式1
    ATIM_OCMODE_COMBINED_PWM2           = (0x1005UL << ATIM_CCMR1CMP_OC1M_Pos), // 组合 PWM 模式2
    ATIM_OCMODE_ASSYMETRIC_PWM1         = (0x1006UL << ATIM_CCMR1CMP_OC1M_Pos), // 不对称 PWM 模式1
    ATIM_OCMODE_ASSYMETRIC_PWM2         = (0x1007UL << ATIM_CCMR1CMP_OC1M_Pos), // 不对称 PWM 模式2
    ATIM_OCMODE_DIRECTION_OUTPUT        = (0x1003UL << ATIM_CCMR1CMP_OC1M_Pos)  // 输出 计数方向
} ATIM_OCModeTypeDef;

/* ATIM 输出比较 输出极性 -----------------------------------------------------*/
typedef enum
{
    ATIM_OCPOLARITY_HIGH                = 0x00000000UL,                         // OCx与OCxDTS同相
    ATIM_OCPOLARITY_LOW                 = ATIM_CCER_CC1P_Msk                    // OCx与OCxDTS反相
} ATIM_OCPolarityTypeDef;

/* ATIM 互补输出比较的输出极性 -------------------------------------------------*/
typedef enum
{
    ATIM_OCNPOLARITY_HIGH               = 0x00000000UL,                         // OCx与OCxNDTS同相
    ATIM_OCNPOLARITY_LOW                = ATIM_CCER_CC1NP_Msk                   // OCx与OCxNDTS反相
} ATIM_OCNPolarityTypeDef;

/* ATIM 输出比较 快速使能 ------------------------------------------------------*/
typedef enum
{
    ATIM_OCFAST_DISABLE                 = 0x00000000UL,                         // 触发启动后OCxREF由OCxM控制
    ATIM_OCFAST_ENABLE                  = ATIM_CCMR1CMP_OC1FE_Msk               // 触发启动后OCxREF尽快变更匹配后的电平
} ATIM_OCFastModeTypeDef;

/* ATIM 输出比较 空闲时状态 ----------------------------------------------------*/
typedef enum
{
    ATIM_OCIDLESTATE_SET                = ATIM_CR2_OIS1_Msk,                    // OCx空闲时输出状态SET
    ATIM_OCIDLESTATE_RESET              = 0x00000000UL                          // OCx空闲时输出状态RESET
} ATIM_OCIdleStateTypeDef;

/* ATIM 互补输出比较 空闲时状态 ------------------------------------------------*/
typedef enum
{
    ATIM_OCNIDLESTATE_SET               = ATIM_CR2_OIS1N_Msk,                   // OCNx空闲时输出状态SET
    ATIM_OCNIDLESTATE_RESET             = 0x00000000UL                          // OCNx空闲时输出状态RESET
} ATIM_OCNIdleStateTypeDef;


/* ATIM 输入极性 --------------------------------------------------------------*/
typedef enum
{
    ATIM_INPUTCHANNELPOLARITY_RISING    = 0x00000000UL,                         // 上升沿触发
    ATIM_INPUTCHANNELPOLARITY_FALLING   = ATIM_CCER_CC1P_Msk,                   // 下降沿触发
    ATIM_INPUTCHANNELPOLARITY_BOTHEDGE  = (ATIM_CCER_CC1P_Msk |
                                           ATIM_CCER_CC1NP_Msk)                 // 双边沿触发
} ATIM_InPutChannelPolarityTypeDef;

/* ATIM 外部触发极性 ----------------------------------------------------------*/
typedef enum
{
    ATIM_ETRPOLARITY_INVERTED           = ATIM_SMCR_ETP_Msk,                    // 反相，ETR的低电平或下降沿有效
    ATIM_ETRPOLARITY_NONINVERTED        = 0x00000000UL                          // 正相，ETR的高电平或上升沿有效
} ATIM_ETRPolarityTypeDef;

/* ATIM 触发极性 --------------------------------------------------------------*/
typedef enum
{
    ATIM_TRIGGERPOLARITY_INVERTED       = ATIM_ETRPOLARITY_INVERTED,            // ETRx外部触发极性
    ATIM_TRIGGERPOLARITY_NONINVERTED    = ATIM_ETRPOLARITY_NONINVERTED,         // ETRx外部触发极性
    ATIM_TRIGGERPOLARITY_RISING         = ATIM_INPUTCHANNELPOLARITY_RISING,     // TIxFPx 或 TI1FED 触发极性
    ATIM_TRIGGERPOLARITY_FALLING        = ATIM_INPUTCHANNELPOLARITY_FALLING,    // TIxFPx 或 TI1FED 触发极性
    ATIM_TRIGGERPOLARITY_BOTHEDGE       = ATIM_INPUTCHANNELPOLARITY_BOTHEDGE    // TIxFPx 或 TI1FED 触发极性
} ATIM_TriggerPolarityTypeDef;

/* ATIM 输入捕获极性 ----------------------------------------------------------*/
typedef enum
{
    ATIM_ICPOLARITY_RISING              = ATIM_INPUTCHANNELPOLARITY_RISING,     // 上升沿触发输入捕获
    ATIM_ICPOLARITY_FALLING             = ATIM_INPUTCHANNELPOLARITY_FALLING,    // 下降沿触发输入捕获
    ATIM_ICPOLARITY_BOTHEDGE            = ATIM_INPUTCHANNELPOLARITY_BOTHEDGE    // 上升下降都触发输入捕获
} ATIM_ICPolarityTypeDef;

/* ATIM 输入捕获通道选择 ------------------------------------------------------*/
typedef enum
{
    ATIM_ICSELECTION_DIRECTTI           = (0x1UL << ATIM_CCMR1CAP_CC1S_Pos),    // IC1,IC2,IC3,IC4,IC5,IC6 映射到
                                                                                // TI1,TI2,TI3,TI4,TI5,TI6 上
    ATIM_ICSELECTION_INDIRECTTI         = (0x2UL << ATIM_CCMR1CAP_CC1S_Pos),    // IC1,IC2,IC3,IC4,IC5,IC6 映射到
                                                                                // TI2,TI1,TI4,TI3,TI6,TI5 上
    ATIM_ICSELECTION_TRGI               = (0x3UL << ATIM_CCMR1CAP_CC1S_Pos)     // IC1,IC2,IC3,IC4,IC5,IC6 映射到TRGI上
} ATIM_ICSelectionTypeDef;

/* ATIM 输入捕获预分频 --------------------------------------------------------*/
typedef enum
{
    ATIM_ICPSC_DIV1                     = 0x00000000UL,                         // 每发生 1 个事件便执行一次捕获
    ATIM_ICPSC_DIV2                     = (0x1UL << ATIM_CCMR1CAP_IC1PSC_Pos),  // 每发生 2 个事件便执行一次捕获
    ATIM_ICPSC_DIV4                     = (0x2UL << ATIM_CCMR1CAP_IC1PSC_Pos),  // 每发生 4 个事件便执行一次捕获
    ATIM_ICPSC_DIV8                     = (0x3UL << ATIM_CCMR1CAP_IC1PSC_Pos)   // 每发生 8 个事件便执行一次捕获
} ATIM_ICPrescalerTypeDef;

/* ATIM 编码器模式 ------------------------------------------------------------*/
typedef enum
{
    ATIM_ENCODERMODE_TI1                      = (0x01UL << ATIM_SMCR_SMS_Pos),  // 正交x2模式, 在TI1FP1边沿计数
    ATIM_ENCODERMODE_TI2                      = (0x02UL << ATIM_SMCR_SMS_Pos),  // 正交x2模式，在TI2FP2边沿计数
    ATIM_ENCODERMODE_TI12                     = (0x03UL << ATIM_SMCR_SMS_Pos),  // 正交x4模式，在TI1FP1、TI2FP2边沿计数
    ATIM_ENCODERMODE_CLOCKPLUSDIRECTION_X2    = (ATIM_SMCR_SMSH_Msk | (0x02UL << ATIM_SMCR_SMS_Pos)), // 方向加脉冲x2模式，在TI2FP2有效边沿计数
    ATIM_ENCODERMODE_CLOCKPLUSDIRECTION_X1    = (ATIM_SMCR_SMSH_Msk | (0x03UL << ATIM_SMCR_SMS_Pos)), // 方向加脉冲x1模式，在TI2FP2有效边沿计数
    ATIM_ENCODERMODE_DIRECTIONALCLOCK_X2      = (ATIM_SMCR_SMSH_Msk | (0x04UL << ATIM_SMCR_SMS_Pos)), // 具有方向的脉冲x2模式，在TI1FP1、TI2FP2有效边沿计数
    ATIM_ENCODERMODE_DIRECTIONALCLOCK_X1_TI12 = (ATIM_SMCR_SMSH_Msk | (0x05UL << ATIM_SMCR_SMS_Pos)), // 具有方向的脉冲x1模式，在TI1FP1、TI2FP2有效边沿计数
    ATIM_ENCODERMODE_X1_TI1                   = (ATIM_SMCR_SMSH_Msk | (0x06UL << ATIM_SMCR_SMS_Pos)), // 正交x1模式，在TI1FP1有效边沿计数
    ATIM_ENCODERMODE_X1_TI2                   = (ATIM_SMCR_SMSH_Msk | (0x07UL << ATIM_SMCR_SMS_Pos))  // 正交x1模式，在TI2FP2有效边沿计数
} ATIM_EnCoderModeTypeDef;

/* ATIM 编码器输入极性 --------------------------------------------------------*/
typedef enum
{
    ATIM_ENCODERINPUTPOLARITY_RISING    = ATIM_INPUTCHANNELPOLARITY_RISING,     // 编码器输入极性
    ATIM_ENCODERINPUTPOLARITY_FALLING   = ATIM_INPUTCHANNELPOLARITY_FALLING     // 编码器输入极性
} ATIM_EnCoderPolarityTypeDef;

/* ATIM 触发源选择 ------------------------------------------------------------*/
typedef enum
{
    ATIM_TS_ITR0    = 0x00000000UL,                                                   // 内部触发0  (ITR0: None)
    ATIM_TS_ITR1    = ((0x00UL << ATIM_SMCR_TSH_Pos) | (0x01UL << ATIM_SMCR_TS_Pos)), // 内部触发1  (ITR1: None)
    ATIM_TS_ITR2    = ((0x00UL << ATIM_SMCR_TSH_Pos) | (0x02UL << ATIM_SMCR_TS_Pos)), // 内部触发2  (ITR2: GTIM1_Trgo)
    ATIM_TS_ITR3    = ((0x00UL << ATIM_SMCR_TSH_Pos) | (0x03UL << ATIM_SMCR_TS_Pos)), // 内部触发3  (ITR3: None)
    ATIM_TS_TI1F_ED = ((0x00UL << ATIM_SMCR_TSH_Pos) | (0x04UL << ATIM_SMCR_TS_Pos)), // TI1 边沿检测器 (TI1FED)
    ATIM_TS_TI1FP1  = ((0x00UL << ATIM_SMCR_TSH_Pos) | (0x05UL << ATIM_SMCR_TS_Pos)), // 滤波后的定时器输入1 (TI1FP1)
    ATIM_TS_TI2FP2  = ((0x00UL << ATIM_SMCR_TSH_Pos) | (0x06UL << ATIM_SMCR_TS_Pos)), // 滤波后的定时器输入2 (TI2FP2)
    ATIM_TS_ETRF    = ((0x00UL << ATIM_SMCR_TSH_Pos) | (0x07UL << ATIM_SMCR_TS_Pos)), // 外部触发输入 (ETRF)
    ATIM_TS_ITR4    = ((0x01UL << ATIM_SMCR_TSH_Pos) | (0x00UL << ATIM_SMCR_TS_Pos)), // 内部触发4  (ITR4: None)
    ATIM_TS_ITR5    = ((0x01UL << ATIM_SMCR_TSH_Pos) | (0x01UL << ATIM_SMCR_TS_Pos)), // 内部触发5  (ITR5: None)
    ATIM_TS_ITR6    = ((0x01UL << ATIM_SMCR_TSH_Pos) | (0x02UL << ATIM_SMCR_TS_Pos)), // 内部触发6  (ITR6: UART1_TXD)
    ATIM_TS_ITR7    = ((0x01UL << ATIM_SMCR_TSH_Pos) | (0x03UL << ATIM_SMCR_TS_Pos)), // 内部触发7  (ITR7: UART2_TXD)
    ATIM_TS_ITR8    = ((0x01UL << ATIM_SMCR_TSH_Pos) | (0x04UL << ATIM_SMCR_TS_Pos)), // 内部触发8  (ITR8: UART1_RXD)
    ATIM_TS_ITR9    = ((0x01UL << ATIM_SMCR_TSH_Pos) | (0x05UL << ATIM_SMCR_TS_Pos)), // 内部触发9  (ITR9: UART2_RXD)
    ATIM_TS_ITR10   = ((0x01UL << ATIM_SMCR_TSH_Pos) | (0x06UL << ATIM_SMCR_TS_Pos)), // 内部触发10 (ITR10: BTIM1_Trgo)
    ATIM_TS_ITR11   = ((0x01UL << ATIM_SMCR_TSH_Pos) | (0x07UL << ATIM_SMCR_TS_Pos)), // 内部触发11 (ITR11: BTIM1_Trgo)
    ATIM_TS_ITR12   = ((0x02UL << ATIM_SMCR_TSH_Pos) | (0x00UL << ATIM_SMCR_TS_Pos)), // 内部触发12 (ITR12: BTIM1_Trgo)
    ATIM_TS_NONE    = ((0x03UL << ATIM_SMCR_TSH_Pos) | (0x07UL << ATIM_SMCR_TS_Pos))  // 无触发选择
} ATIM_InputTriggerTypeDef;

/* ATIM 计数时钟源 ------------------------------------------------------------*/
typedef enum
{
    ATIM_CLOCKSOURCE_ETRMODE2           = (0x2UL << ATIM_SMCR_ETPS_Pos),        // 外部时钟源模式2
    ATIM_CLOCKSOURCE_INTERNAL           = (0x1UL << ATIM_SMCR_ETPS_Pos),        // 内部时钟源模式
    ATIM_CLOCKSOURCE_ITR0               = ATIM_TS_ITR0,                         // 外部时钟源模式1 (ITR0: None)
    ATIM_CLOCKSOURCE_ITR1               = ATIM_TS_ITR1,                         // 外部时钟源模式1 (ITR1: None)
    ATIM_CLOCKSOURCE_ITR2               = ATIM_TS_ITR2,                         // 外部时钟源模式1 (ITR2: GTIM1_Trgo)
    ATIM_CLOCKSOURCE_ITR3               = ATIM_TS_ITR3,                         // 外部时钟源模式1 (ITR3: None)
    ATIM_CLOCKSOURCE_TI1ED              = ATIM_TS_TI1F_ED,                      // 外部时钟源模式1 (TI1FED)
    ATIM_CLOCKSOURCE_TI1                = ATIM_TS_TI1FP1,                       // 外部时钟源模式1 (TI1FP1)
    ATIM_CLOCKSOURCE_TI2                = ATIM_TS_TI2FP2,                       // 外部时钟源模式1 (TI2FP2)
    ATIM_CLOCKSOURCE_ETRMODE1           = ATIM_TS_ETRF,                         // 外部时钟源模式1 (ETRF)
    ATIM_CLOCKSOURCE_ITR4               = ATIM_TS_ITR4,                         // 外部时钟源模式1 (ITR4: None)
    ATIM_CLOCKSOURCE_ITR5               = ATIM_TS_ITR5,                         // 外部时钟源模式1 (ITR5: None)
    ATIM_CLOCKSOURCE_ITR6               = ATIM_TS_ITR6,                         // 外部时钟源模式1 (ITR6: UART1_TXD)
    ATIM_CLOCKSOURCE_ITR7               = ATIM_TS_ITR7,                         // 外部时钟源模式1 (ITR7: UART2_TXD)
    ATIM_CLOCKSOURCE_ITR8               = ATIM_TS_ITR8,                         // 外部时钟源模式1 (ITR8: UART1_RXD)
    ATIM_CLOCKSOURCE_ITR9               = ATIM_TS_ITR9,                         // 外部时钟源模式1 (ITR9: UART2_RXD)
    ATIM_CLOCKSOURCE_ITR10              = ATIM_TS_ITR10,                        // 外部时钟源模式1 (ITR10: BTIM1_Trgo)
    ATIM_CLOCKSOURCE_ITR11              = ATIM_TS_ITR11,                        // 外部时钟源模式1 (ITR11: BTIM1_Trgo)
    ATIM_CLOCKSOURCE_ITR12              = ATIM_TS_ITR12                         // 外部时钟源模式1 (ITR12: BTIM1_Trgo)
} ATIM_ClockSourceTypeDef;

/* ATIM 计数时钟极性 --------------------------------------------------------*/
typedef enum
{
    ATIM_CLOCKPOLARITY_INVERTED         = ATIM_ETRPOLARITY_INVERTED,            // ETRx 极性反相
    ATIM_CLOCKPOLARITY_NONINVERTED      = ATIM_ETRPOLARITY_NONINVERTED,         // ETRx 极性不反相
    ATIM_CLOCKPOLARITY_RISING           = ATIM_INPUTCHANNELPOLARITY_RISING,     // ITx 上升沿计数
    ATIM_CLOCKPOLARITY_FALLING          = ATIM_INPUTCHANNELPOLARITY_FALLING,    // ITx 下降沿计数
    ATIM_CLOCKPOLARITY_BOTHEDGE         = ATIM_INPUTCHANNELPOLARITY_BOTHEDGE    // ITx 双边沿计数
} ATIM_ClockPolarityTypeDef;

/* ATIM ETR信号预分频 --------------------------------------------------------*/
typedef enum
{
    ATIM_ETRPRESCALER_DIV1              = 0x00000000UL,                         // fETRP = fETR
    ATIM_ETRPRESCALER_DIV2              = (0x1UL << ATIM_SMCR_ETPS_Pos),        // fETRP = fETR / 2
    ATIM_ETRPRESCALER_DIV4              = (0x2UL << ATIM_SMCR_ETPS_Pos),        // fETRP = fETR / 4
    ATIM_ETRPRESCALER_DIV8              = (0x3UL << ATIM_SMCR_ETPS_Pos)         // fETRP = fETR / 8
} ATIM_ETRPrescalerTypeDef;

/* ATIM 计数时钟预分频 --------------------------------------------------------*/
typedef enum
{
    ATIM_CLOCKPRESCALER_DIV1            = ATIM_ETRPRESCALER_DIV1,               // 不分频
    ATIM_CLOCKPRESCALER_DIV2            = ATIM_ETRPRESCALER_DIV2,               // 2 分频
    ATIM_CLOCKPRESCALER_DIV4            = ATIM_ETRPRESCALER_DIV4,               // 4 分频
    ATIM_CLOCKPRESCALER_DIV8            = ATIM_ETRPRESCALER_DIV8,               // 8 分频
} ATIM_ClockPrescalerTypeDef;

/* ATIM OCREF_CLR信号来源 -----------------------------------------------------*/
typedef enum
{
    ATIM_CLEARINPUTSOURCE_NONE          = 0x7FFFFFFFUL,                         // 禁止OCREF_CLR
    ATIM_CLEARINPUTSOURCE_ETR           = 0x00000001UL,                         // OCREF_CLR = ETRF输入
    ATIM_CLEARINPUTSOURCE_VC1_OUT       = (0x0UL << ATIM_AF2_OCRSEL_Pos),       // OCREF_CLR = VC1_OUT
    ATIM_CLEARINPUTSOURCE_VC2_OUT       = (0x1UL << ATIM_AF2_OCRSEL_Pos),       // OCREF_CLR = VC2_OUT
    ATIM_CLEARINPUTSOURCE_ADC_AWD       = (0x4UL << ATIM_AF2_OCRSEL_Pos)        // OCREF_CLR = ADC_AWD
} ATIM_ClearInputSourceTypeDef;

/* ATIM OCREF清零信号极性 -----------------------------------------------------*/
typedef enum
{
    ATIM_CLEARINPUTPOLARITY_INVERTED    = ATIM_ETRPOLARITY_INVERTED,            // ETRx 管脚极性
    ATIM_CLEARINPUTPOLARITY_NONINVERTED = ATIM_ETRPOLARITY_NONINVERTED,         // ETRx 管脚极性
} ATIM_ClearInputPolarityTypeDef;

/* ATIM OCREF清零信号预分频 ----------------------------------------------------*/
typedef enum
{
    ATIM_CLEARINPUTPRESCALER_DIV1       = ATIM_ETRPRESCALER_DIV1,               // ETRx 管脚预分频
    ATIM_CLEARINPUTPRESCALER_DIV2       = ATIM_ETRPRESCALER_DIV2,               // ETRx 管脚预分频
    ATIM_CLEARINPUTPRESCALER_DIV4       = ATIM_ETRPRESCALER_DIV4,               // ETRx 管脚预分频
    ATIM_CLEARINPUTPRESCALER_DIV8       = ATIM_ETRPRESCALER_DIV8,               // ETRx 管脚预分频
} ATIM_ClearInputPrescalerTypeDef;

/* ATIM TRGO 选择 ------------------------------------------------------------*/
typedef enum
{
    ATIM_TRGO_RESET                     = 0x00000000UL,                                                   // ATIM_EGR.UG 被用作 Trigger Out(TRGO)
    ATIM_TRGO_ENABLE                    = ((0x00UL << ATIM_CR2_MMSH_Pos) | (0x01UL << ATIM_CR2_MMS_Pos)), // ATIM_CR1.CEN 位被用作 Trigger Out(TRGO)
    ATIM_TRGO_UPDATE                    = ((0x00UL << ATIM_CR2_MMSH_Pos) | (0x02UL << ATIM_CR2_MMS_Pos)), // Update event 被用作 Trigger Out(TRGO)
    ATIM_TRGO_ENCODER_CLK               = ((0x00UL << ATIM_CR2_MMSH_Pos) | (0x03UL << ATIM_CR2_MMS_Pos)), // Encoder clock 被用作 Trigger Out(TRGO)
    ATIM_TRGO_CC1                       = ((0x00UL << ATIM_CR2_MMSH_Pos) | (0x04UL << ATIM_CR2_MMS_Pos)), // Capture1 或 compare1 match 被用作 Trigger Out(TRGO)
    ATIM_TRGO_CC2                       = ((0x00UL << ATIM_CR2_MMSH_Pos) | (0x05UL << ATIM_CR2_MMS_Pos)), // Capture2 或 compare2 match 被用作 Trigger Out(TRGO)
    ATIM_TRGO_CC3                       = ((0x00UL << ATIM_CR2_MMSH_Pos) | (0x06UL << ATIM_CR2_MMS_Pos)), // Capture3 或 compare3 match 被用作 Trigger Out(TRGO)
    ATIM_TRGO_CC4                       = ((0x00UL << ATIM_CR2_MMSH_Pos) | (0x07UL << ATIM_CR2_MMS_Pos)), // Capture4 或 compare4 match 被用作 Trigger Out(TRGO)
    ATIM_TRGO_CC5                       = ((0x01UL << ATIM_CR2_MMSH_Pos) | (0x00UL << ATIM_CR2_MMS_Pos)), // Capture5 或 compare5 match 被用作 Trigger Out(TRGO)
    ATIM_TRGO_CC6                       = ((0x01UL << ATIM_CR2_MMSH_Pos) | (0x01UL << ATIM_CR2_MMS_Pos)), // Capture6 或 compare6 match 被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC1REF                    = ((0x01UL << ATIM_CR2_MMSH_Pos) | (0x02UL << ATIM_CR2_MMS_Pos)), // OC1REF 信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC2REF                    = ((0x01UL << ATIM_CR2_MMSH_Pos) | (0x03UL << ATIM_CR2_MMS_Pos)), // OC2REF 信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC3REF                    = ((0x01UL << ATIM_CR2_MMSH_Pos) | (0x04UL << ATIM_CR2_MMS_Pos)), // OC3REF 信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC4REF                    = ((0x01UL << ATIM_CR2_MMSH_Pos) | (0x05UL << ATIM_CR2_MMS_Pos)), // OC4REF 信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC5REF                    = ((0x01UL << ATIM_CR2_MMSH_Pos) | (0x06UL << ATIM_CR2_MMS_Pos)), // OC5REF 信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC6REF                    = ((0x01UL << ATIM_CR2_MMSH_Pos) | (0x07UL << ATIM_CR2_MMS_Pos)), // OC6REF 信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC1REFC                   = ((0x02UL << ATIM_CR2_MMSH_Pos) | (0x00UL << ATIM_CR2_MMS_Pos)), // OC1REFC信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC2REFC                   = ((0x02UL << ATIM_CR2_MMSH_Pos) | (0x01UL << ATIM_CR2_MMS_Pos)), // OC2REFC信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC3REFC                   = ((0x02UL << ATIM_CR2_MMSH_Pos) | (0x02UL << ATIM_CR2_MMS_Pos)), // OC3REFC信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC4REFC                   = ((0x02UL << ATIM_CR2_MMSH_Pos) | (0x03UL << ATIM_CR2_MMS_Pos)), // OC4REFC信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC5REFC                   = ((0x02UL << ATIM_CR2_MMSH_Pos) | (0x04UL << ATIM_CR2_MMS_Pos)), // OC5REFC信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC6REFC                   = ((0x02UL << ATIM_CR2_MMSH_Pos) | (0x05UL << ATIM_CR2_MMS_Pos)), // OC6REFC信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC3REFC_BOTHEDGE          = ((0x02UL << ATIM_CR2_MMSH_Pos) | (0x06UL << ATIM_CR2_MMS_Pos)), // OC3REFC上升边沿或下降边沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC4REFC_BOTHEDGE          = ((0x02UL << ATIM_CR2_MMSH_Pos) | (0x07UL << ATIM_CR2_MMS_Pos)), // OC4REFC上升边沿或下降边沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC5REFC_BOTHEDGE          = ((0x03UL << ATIM_CR2_MMSH_Pos) | (0x00UL << ATIM_CR2_MMS_Pos)), // OC5REFC上升边沿或下降边沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC6REFC_BOTHEDGE          = ((0x03UL << ATIM_CR2_MMSH_Pos) | (0x01UL << ATIM_CR2_MMS_Pos)), // OC6REFC上升边沿或下降边沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC4REFC_RISE_OC5REFC_RISE = ((0x03UL << ATIM_CR2_MMSH_Pos) | (0x02UL << ATIM_CR2_MMS_Pos)), // OC4REFC上升沿和OC5REFC上升沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC4REFC_RISE_OC5REFC_FALL = ((0x03UL << ATIM_CR2_MMSH_Pos) | (0x03UL << ATIM_CR2_MMS_Pos)), // OC4REFC上升沿和OC5REFC下降沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC4REFC_RISE_OC6REFC_RISE = ((0x03UL << ATIM_CR2_MMSH_Pos) | (0x04UL << ATIM_CR2_MMS_Pos)), // OC4REFC上升沿和OC6REFC上升沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC4REFC_RISE_OC6REFC_FALL = ((0x03UL << ATIM_CR2_MMSH_Pos) | (0x05UL << ATIM_CR2_MMS_Pos)), // OC4REFC上升沿和OC6REFC下降沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC5REFC_RISE_OC6REFC_RISE = ((0x03UL << ATIM_CR2_MMSH_Pos) | (0x06UL << ATIM_CR2_MMS_Pos)), // OC5REFC上升沿和OC6REFC上升沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO_OC5REFC_RISE_OC6REFC_FALL = ((0x03UL << ATIM_CR2_MMSH_Pos) | (0x07UL << ATIM_CR2_MMS_Pos))  // OC5REFC上升沿和OC6REFC下降沿信号被用作 Trigger Out(TRGO)
} ATIM_TrgoSelectionTypeDef;

/* ATIM TRGO2 选择 ------------------------------------------------------------*/
typedef enum
{
    ATIM_TRGO2_RESET                     = 0x00000000UL,                        // ATIM_EGR.UG 被用作 Trigger Out(TRGO)
    ATIM_TRGO2_ENABLE                    = (0x01UL << ATIM_CR2_MMS2_Pos),       // ATIM_CR1.CEN 位被用作 Trigger Out(TRGO)
    ATIM_TRGO2_UPDATE                    = (0x02UL << ATIM_CR2_MMS2_Pos),       // Update event 被用作 Trigger Out(TRGO)
    ATIM_TRGO2_ENCODER_CLK               = (0x03UL << ATIM_CR2_MMS2_Pos),       // Encoder clock 被用作 Trigger Out(TRGO)
    ATIM_TRGO2_CC1                       = (0x04UL << ATIM_CR2_MMS2_Pos),       // Capture1 或 compare1 match 1 被用作 Trigger Out(TRGO)
    ATIM_TRGO2_CC2                       = (0x05UL << ATIM_CR2_MMS2_Pos),       // Capture2 或 compare2 match 1 被用作 Trigger Out(TRGO)
    ATIM_TRGO2_CC3                       = (0x06UL << ATIM_CR2_MMS2_Pos),       // Capture3 或 compare3 match 1 被用作 Trigger Out(TRGO)
    ATIM_TRGO2_CC4                       = (0x07UL << ATIM_CR2_MMS2_Pos),       // Capture4 或 compare4 match 1 被用作 Trigger Out(TRGO)
    ATIM_TRGO2_CC5                       = (0x08UL << ATIM_CR2_MMS2_Pos),       // Capture5 或 compare5 match 1 被用作 Trigger Out(TRGO)
    ATIM_TRGO2_CC6                       = (0x09UL << ATIM_CR2_MMS2_Pos),       // Capture6 或 compare6 match 1 被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC1REF                    = (0x0AUL << ATIM_CR2_MMS2_Pos),       // OC1REF 信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC2REF                    = (0x0BUL << ATIM_CR2_MMS2_Pos),       // OC2REF 信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC3REF                    = (0x0CUL << ATIM_CR2_MMS2_Pos),       // OC3REF 信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC4REF                    = (0x0DUL << ATIM_CR2_MMS2_Pos),       // OC4REF 信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC5REF                    = (0x0EUL << ATIM_CR2_MMS2_Pos),       // OC5REF 信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC6REF                    = (0x0FUL << ATIM_CR2_MMS2_Pos),       // OC6REF 信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC1REFC                   = (0x10UL << ATIM_CR2_MMS2_Pos),       // OC1REFC信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC2REFC                   = (0x11UL << ATIM_CR2_MMS2_Pos),       // OC2REFC信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC3REFC                   = (0x12UL << ATIM_CR2_MMS2_Pos),       // OC3REFC信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC4REFC                   = (0x13UL << ATIM_CR2_MMS2_Pos),       // OC4REFC信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC5REFC                   = (0x14UL << ATIM_CR2_MMS2_Pos),       // OC5REFC信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC6REFC                   = (0x15UL << ATIM_CR2_MMS2_Pos),       // OC6REFC信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC3REFC_BOTHEDGE          = (0x16UL << ATIM_CR2_MMS2_Pos),       // OC3REFC上升边沿或下降边沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC4REFC_BOTHEDGE          = (0x17UL << ATIM_CR2_MMS2_Pos),       // OC4REFC上升边沿或下降边沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC5REFC_BOTHEDGE          = (0x18UL << ATIM_CR2_MMS2_Pos),       // OC5REFC上升边沿或下降边沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC6REFC_BOTHEDGE          = (0x19UL << ATIM_CR2_MMS2_Pos),       // OC6REFC上升边沿或下降边沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC4REFC_RISE_OC5REFC_RISE = (0x1AUL << ATIM_CR2_MMS2_Pos),       // OC4REFC上升沿和OC5REFC上升沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC4REFC_RISE_OC5REFC_FALL = (0x1BUL << ATIM_CR2_MMS2_Pos),       // OC4REFC上升沿和OC5REFC下降沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC4REFC_RISE_OC6REFC_RISE = (0x1CUL << ATIM_CR2_MMS2_Pos),       // OC4REFC上升沿和OC6REFC上升沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC4REFC_RISE_OC6REFC_FALL = (0x1DUL << ATIM_CR2_MMS2_Pos),       // OC4REFC上升沿和OC6REFC下降沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC5REFC_RISE_OC6REFC_RISE = (0x1EUL << ATIM_CR2_MMS2_Pos),       // OC5REFC上升沿和OC6REFC上升沿信号被用作 Trigger Out(TRGO)
    ATIM_TRGO2_OC5REFC_RISE_OC6REFC_FALL = (0x1FUL << ATIM_CR2_MMS2_Pos)        // OC5REFC上升沿和OC6REFC下降沿信号被用作 Trigger Out(TRGO)
} ATIM_Trgo2SelectionTypeDef;

/* ATIM 主/从模式 ------------------------------------------------------------*/
typedef enum
{
    ATIM_MASTERSLAVEMODE_ENABLE         =  ATIM_SMCR_MSM_Msk,                   // 主/从模式被选中
    ATIM_MASTERSLAVEMODE_DISABLE        =  0x00000000UL                         // 不进行任何操作
} ATIM_MasterSlaveModeTypeDef;

/* ATIM 从模式选择 ------------------------------------------------------------*/
typedef enum
{
    ATIM_SLAVEMODE_DISABLE               = 0x00000000UL,                        // 禁止从模式
    ATIM_SLAVEMODE_RESET                 = (0x04UL << ATIM_SMCR_SMS_Pos),       // 复位模式
    ATIM_SLAVEMODE_GATED                 = (0x05UL << ATIM_SMCR_SMS_Pos),       // 门控模式
    ATIM_SLAVEMODE_TRIGGER               = (0x06UL << ATIM_SMCR_SMS_Pos),       // 触发模式
    ATIM_SLAVEMODE_EXTERNAL1             = (0x07UL << ATIM_SMCR_SMS_Pos),       // 外部时钟模式1
    ATIM_SLAVEMODE_COMBINED_RESETTRIGGER = (ATIM_SMCR_SMSH_Msk | (0x00UL << ATIM_SMCR_SMS_Pos)), // 组合复位+触发模式
    ATIM_SLAVEMODE_COMBINED_GATEDRESET   = (ATIM_SMCR_SMSH_Msk | (0x01UL << ATIM_SMCR_SMS_Pos)), // 组合复位+门控模式
} ATIM_SlaveModeTypeDef;

/* ATIM 从模式触发信号预分频 ---------------------------------------------------*/
typedef enum
{
    ATIM_TRIGGERPRESCALER_DIV1          = ATIM_ETRPRESCALER_DIV1,               // 每 1 次事件触发一次
    ATIM_TRIGGERPRESCALER_DIV2          = ATIM_ETRPRESCALER_DIV2,               // 每 2 次事件触发一次
    ATIM_TRIGGERPRESCALER_DIV4          = ATIM_ETRPRESCALER_DIV4,               // 每 4 次事件触发一次
    ATIM_TRIGGERPRESCALER_DIV8          = ATIM_ETRPRESCALER_DIV8                // 每 8 次事件触发一次
} ATIM_TriggerPrescalerTypeDef;

/* ATIM 运行模式下的关闭状态管脚状态 -------------------------------------------*/
typedef enum
{
    ATIM_OSSR_ENABLE                    = ATIM_BDTR_OSSR_Msk,                   // 无效状态时, 由ATIM控制OCx/OCxN管脚，输出无效电平
    ATIM_OSSR_DISABLE                   = 0x00000000UL                          // 无效状态时, 由GPIO寄存器控制OCx/OCxN管脚
} ATIM_OffStateRunModeTypeDef;

/* ATIM 空闲模式下的关闭状态管脚状态 -------------------------------------------*/
typedef enum
{
    ATIM_OSSI_ENABLE                    = ATIM_BDTR_OSSI_Msk,                   // 无效状态时, 由ATIM控制OCx/OCxN管脚，先输出无效电平
    ATIM_OSSI_DISABLE                   = 0x00000000UL                          // 无效状态时, 由GPIO寄存器控制OCx/OCxN管脚
} ATIM_OffStateIDLEModeTypeDef;

/* ATIM 寄存器位域锁定 --------------------------------------------------------*/
typedef enum
{
    ATIM_LOCKLEVEL_OFF                  = 0x00000000UL,                         // 禁止锁定，所有寄存器可任意修改
    ATIM_LOCKLEVEL_1                    = (0x01UL << ATIM_BDTR_LOCK_Pos),       // 锁定级别1
    ATIM_LOCKLEVEL_2                    = (0x02UL << ATIM_BDTR_LOCK_Pos),       // 锁定级别2
    ATIM_LOCKLEVEL_3                    = (0x03UL << ATIM_BDTR_LOCK_Pos)        // 锁定级别3
} ATIM_LockLevelTypeDef;

/* ATIM 刹车输入使能控制 ------------------------------------------------------*/
typedef enum
{
    ATIM_BREAK_ENABLE                   = ATIM_BDTR_BKE_Msk,                    // 刹车输入使能
    ATIM_BREAK_DISABLE                  = 0x00000000UL                          // 刹车输入禁止
} ATIM_BreakStateTypeDef;

/* ATIM 刹车输入极性 ----------------------------------------------------------*/
typedef enum
{
    ATIM_BREAKPOLARITY_LOW              = 0x00000000UL,                         // 刹车输入低电平有效
    ATIM_BREAKPOLARITY_HIGH             = ATIM_BDTR_BKP_Msk                     // 刹车输入高电平有效
} ATIM_BreakPolarityTypeDef;

/* ATIM 刹车输入2使能控制 -----------------------------------------------------*/
typedef enum
{
    ATIM_BREAK2_ENABLE                  = ATIM_BDTR_BK2E_Msk,                   // 刹车输入2使能
    ATIM_BREAK2_DISABLE                 = 0x00000000UL                          // 刹车输入2禁止
} ATIM_Break2StateTypeDef;

/* ATIM 刹车输入2极性 ---------------------------------------------------------*/
typedef enum
{
    ATIM_BREAK2POLARITY_LOW             = 0x00000000UL,                         // 刹车输入2低电平有效
    ATIM_BREAK2POLARITY_HIGH            = ATIM_BDTR_BK2P_Msk                    // 刹车输入2高电平有效
} ATIM_Break2PolarityTypeDef;

/* ATIM 自动输出使能控制 ------------------------------------------------------*/
typedef enum
{
    ATIM_AUTOMATICOUTPUT_DISABLE        = 0x00000000UL,                         // 禁止自动输出使能，MOE只能软件置1
    ATIM_AUTOMATICOUTPUT_ENABLE         = ATIM_BDTR_AOE_Msk                     // 开启自动输出使能，刹车信号（BK或BK2）无效且发生UEV时自动将MOE置1
} ATIM_AutomaticOutputTypeDef;

/* ATIM 刹车输入信号来源 ------------------------------------------------------*/
typedef enum
{
    ATIM_BREAKINPUTSOURCE_BKIN          = 0x00000001UL,                         // BKIN 管脚
    ATIM_BREAKINPUTSOURCE_VC1           = 0x00000002UL,                         // VC1_OUT
    ATIM_BREAKINPUTSOURCE_VC2           = 0x00000004UL                          // VC2_OUT
} ATIM_BreakSourceTypeDef;

/* ATIM 刹车输入源使能控制 -----------------------------------------------------*/
typedef enum
{
    ATIM_BREAKINPUTSOURCE_DISABLE       = 0x00000000UL,                         // 禁止刹车输入源
    ATIM_BREAKINPUTSOURCE_ENABLE        = 0x00000001UL                          // 使能刹车输入源
} ATIM_BreakSourceEnTypeDef;

/* ATIM 刹车输入源极性控制 -----------------------------------------------------*/
typedef enum
{
    ATIM_BREAKINPUTSOURCE_POLARITY_LOW  = 0x00000001UL,                         // 刹车输入信号低电平有效
    ATIM_BREAKINPUTSOURCE_POLARITY_HIGH = 0x00000000UL                          // 刹车输入信号高电平有效
} ATIM_BreakSourcePolarityTypeDef;

/* ATIM 编码器索引信号极性 -----------------------------------------------------*/
typedef enum
{
    ATIM_ENCODERINDEX_POLARITY_INVERTED    = ATIM_ETRPOLARITY_INVERTED,         // 编码器索引信号的极性
    ATIM_ENCODERINDEX_POLARITY_NONINVERTED = ATIM_ETRPOLARITY_NONINVERTED       // 编码器索引信号的极性
} ATIM_IndexPolarityTypeDef;

/* ATIM 编码器索引信号预分频 ---------------------------------------------------*/
typedef enum
{
    ATIM_ENCODERINDEX_PRESCALER_DIV1    = ATIM_ETRPRESCALER_DIV1,               // 不分频
    ATIM_ENCODERINDEX_PRESCALER_DIV2    = ATIM_ETRPRESCALER_DIV2,               // 2分频
    ATIM_ENCODERINDEX_PRESCALER_DIV4    = ATIM_ETRPRESCALER_DIV4,               // 4分频
    ATIM_ENCODERINDEX_PRESCALER_DIV8    = ATIM_ETRPRESCALER_DIV8                // 8分频
} ATIM_IndexPrescalerTypeDef;

/* ATIM 编码器索引信号的有效电平(索引位置) --------------------------------------*/
typedef enum
{
    ATIM_ENCODERINDEX_POSITION_00       = 0x00000000UL,                         // 正交编码模式时, 索引位置 AB=00, A = TI1FP1, B = TI2FP2
    ATIM_ENCODERINDEX_POSITION_01       = (0x01UL << ATIM_ECR_IPOS_Pos),        // 正交编码模式时, 索引位置 AB=01
    ATIM_ENCODERINDEX_POSITION_10       = (0x02UL << ATIM_ECR_IPOS_Pos),        // 正交编码模式时, 索引位置 AB=10
    ATIM_ENCODERINDEX_POSITION_11       = (0x03UL << ATIM_ECR_IPOS_Pos),        // 正交编码模式时, 索引位置 AB=11
    ATIM_ENCODERINDEX_POSITION_0        = 0x00000000UL,                         // 方向加脉冲模式时, 时钟低电平时重置计数器
    ATIM_ENCODERINDEX_POSITION_1        = (0x01UL << ATIM_ECR_IPOS_Pos)         // 方向加脉冲模式时, 时钟高电平时重置计数器
} ATIM_IndexPositionTypeDef;

/* ATIM 编码器索引信号有效方向 -------------------------------------------------*/
typedef enum
{
    ATIM_ENCODERINDEX_DIRECTION_UP_DOWN = 0x00000000UL,                         // 任意方向索引有效
    ATIM_ENCODERINDEX_DIRECTION_UP      = (0x01UL << ATIM_ECR_IDIR_Pos),        // 仅向上计数时索引有效
    ATIM_ENCODERINDEX_DIRECTION_DOWN    = (0x02UL << ATIM_ECR_IDIR_Pos)         // 仅向下计数时索引有效
} ATIM_IndexDirectionTypeDef;

/* ATIM 活跃通道 ------------------------------------------------------*/
typedef enum
{
    HAL_ATIM_ACTIVE_CHANNEL_1           = 0x01UL,                               // 活跃的通道为 1
    HAL_ATIM_ACTIVE_CHANNEL_2           = 0x02UL,                               // 活跃的通道为 2
    HAL_ATIM_ACTIVE_CHANNEL_3           = 0x04UL,                               // 活跃的通道为 3
    HAL_ATIM_ACTIVE_CHANNEL_4           = 0x08UL,                               // 活跃的通道为 4
    HAL_ATIM_ACTIVE_CHANNEL_5           = 0x10UL,                               // 活跃的通道为 5
    HAL_ATIM_ACTIVE_CHANNEL_6           = 0x20UL,                               // 活跃的通道为 6
    HAL_ATIM_ACTIVE_CHANNEL_CLEARED     = 0x00UL                                // 清除所有活跃的通道
} HAL_ATIM_ActiveChannel;

/* ATIM 时基初始化结构体 ------------------------------------------------------*/
typedef struct
{
    uint32_t                            Prescaler;                              // 预分频值 0x0000~0xFFFF
    ATIM_CountModeTypeDef               CounterMode;                            // 计数模式
    uint32_t                            Period;                                 // 自动重载值 0x0000~0xFFFF
    ATIM_ClockDivisionTypeDef           ClockDivision;                          // 滤波时钟分频比
    uint32_t                            RepetitionCounter;                      // 重复计数值，计数器每 RCR+1 次溢出，触发一次更新事件
    ATIM_AutoReloadPreloadTypeDef       AutoReloadPreload;                      // 自动重载值的预装载功能使能控制
} ATIM_Base_InitTypeDef;

/* ATIM 输出比较 配置结构体 ----------------------------------------------------*/
typedef struct
{
    ATIM_OCModeTypeDef                  OCMode;                                 // ATIM 输出比较的输出模式
    uint32_t                            Pulse;                                  // 向捕获比较寄存器装载的值 0x0000~0xFFFF
    ATIM_OCPolarityTypeDef              OCPolarity;                             // 输出极性
    ATIM_OCNPolarityTypeDef             OCNPolarity;                            // 互补输出极性
    ATIM_OCFastModeTypeDef              OCFastMode;                             // 输出快速使能控制，仅单脉冲模式适用
    ATIM_OCIdleStateTypeDef             OCIdleState;                            // 输出比较管脚在空闲时的状态
    ATIM_OCNIdleStateTypeDef            OCNIdleState;                           // 互补输出比较管脚在空闲时的状态
} ATIM_OC_InitTypeDef;

/* ATIM 单脉冲模式配置结构体 ---------------------------------------------------*/
typedef struct
{
    ATIM_OCModeTypeDef                  OCMode;                                 // ATIM 输出比较的输出模式
    uint32_t                            Pulse;                                  // 脉冲宽度 向捕获比较寄存器装载的值 0x0000~0xFFFF
    ATIM_OCPolarityTypeDef              OCPolarity;                             // 输出极性
    ATIM_OCNPolarityTypeDef             OCNPolarity;                            // 互补输出极性
    ATIM_OCIdleStateTypeDef             OCIdleState;                            // 输出比较管脚在空闲时的状态
    ATIM_OCNIdleStateTypeDef            OCNIdleState;                           // 互补输出比较管脚在空闲时的状态
    ATIM_ICPolarityTypeDef              ICPolarity;                             // 输入捕获信号极性
    ATIM_ICSelectionTypeDef             ICSelection;                            // 输入捕获通道选择
    uint32_t                            ICFilter;                               // 输入捕获信号滤波参数 0x0~0xF
} ATIM_OnePulse_InitTypeDef;

/* ATIM 输入捕获配置结构体 -----------------------------------------------------*/
typedef struct
{
  ATIM_ICPolarityTypeDef                ICPolarity;                             // 输入捕获信号极性
  ATIM_ICSelectionTypeDef               ICSelection;                            // 输入捕获通道选择
  ATIM_ICPrescalerTypeDef               ICPrescaler;                            // 输入捕获信号预分频
  uint32_t                              ICFilter;                               // 输入捕获信号滤波参数 0x0~0xF
} ATIM_IC_InitTypeDef;

/* ATIM 编码器配置结构体 ------------------------------------------------------*/
typedef struct
{
    ATIM_EnCoderModeTypeDef             EncoderMode;                            // 编码器模式

    ATIM_EnCoderPolarityTypeDef         IC1Polarity;                            // 通道极性
    ATIM_ICSelectionTypeDef             IC1Selection;                           // 通道选择
    ATIM_ICPrescalerTypeDef             IC1Prescaler;                           // 捕获预分频
    uint32_t                            IC1Filter;                              // 滤波参数

    ATIM_EnCoderPolarityTypeDef         IC2Polarity;                            // 通道极性
    ATIM_ICSelectionTypeDef             IC2Selection;                           // 通道选择
    ATIM_ICPrescalerTypeDef             IC2Prescaler;                           // 捕获预分频
    uint32_t                            IC2Filter;                              // 滤波参数
} ATIM_Encoder_InitTypeDef;

/* ATIM 计数时钟配置结构体 -----------------------------------------------------*/
typedef struct
{
    ATIM_ClockSourceTypeDef             ClockSource;                            // ATIM 计数时钟源
    ATIM_ClockPolarityTypeDef           ClockPolarity;                          // ATIM 计数时钟极性
    ATIM_ClockPrescalerTypeDef          ClockPrescaler;                         // ATIM 计数时钟预分频
    uint32_t                            ClockFilter;                            // 计数时钟滤波参数
} ATIM_ClockConfigTypeDef;

/* ATIM 清零输入配置结构体 -----------------------------------------------------*/
typedef struct
{
    FunctionalState                     ClearInputState;                        // OCREF_CLR使能状态 ENABLE / DISABLE
    ATIM_ClearInputSourceTypeDef        ClearInputSource;                       // OCREF_CLR信号来源
    ATIM_ClearInputPolarityTypeDef      ClearInputPolarity;                     // OCREF_CLR信号极性
    ATIM_ClearInputPrescalerTypeDef     ClearInputPrescaler;                    // OCREF_CLR信号预分频
    uint32_t                            ClearInputFilter;                       // OCREF_CLR信号滤波参数 0x0~0xF
} ATIM_ClearInputConfigTypeDef;

/* ATIM 主模式配置结构体 ------------------------------------------------------*/
typedef struct
{
    ATIM_TrgoSelectionTypeDef           MasterOutputTrigger;                    // Trigger output (TRGO) 信号选择
    ATIM_Trgo2SelectionTypeDef          MasterOutputTrigger2;                   // Trigger output2 (TRGO2) 信号选择
    ATIM_MasterSlaveModeTypeDef         MasterSlaveMode;                        // 主从模式配置
} ATIM_MasterConfigTypeDef;

/* ATIM 从模式配置结构体 ------------------------------------------------------*/
typedef struct
{
    ATIM_SlaveModeTypeDef               SlaveMode;                              // 从模式选择
    ATIM_InputTriggerTypeDef            InputTrigger;                           // 输入触发源
    ATIM_TriggerPolarityTypeDef         TriggerPolarity;                        // 触发极性
    ATIM_TriggerPrescalerTypeDef        TriggerPrescaler;                       // 输入触发预分频
    uint32_t                            TriggerFilter;                          // 输入触发滤波参数 0x0~0xF
} ATIM_SlaveConfigTypeDef;

/* ATIM 刹车输入和死区配置结构体 -----------------------------------------------*/
typedef struct
{
    ATIM_OffStateRunModeTypeDef         OffStateRunMode;                        // ATIM 运行模式下的关闭状态管脚状态配置
    ATIM_OffStateIDLEModeTypeDef        OffStateIDLEMode;                       // ATIM 空闲模式下的关闭状态管脚状态配置
    ATIM_LockLevelTypeDef               LockLevel;                              // ATIM 寄存器位域锁定配置
    uint32_t                            DeadTime;                               // ATIM 死区大小 0x00~0xFF
    ATIM_BreakStateTypeDef              BreakState;                             // ATIM 刹车输入使能状态控制
    ATIM_BreakPolarityTypeDef           BreakPolarity;                          // ATIM 刹车输入极性控制
    uint32_t                            BreakFilter;                            // ATIM 刹车输入滤波参数 0x0~0xF
    ATIM_Break2StateTypeDef             Break2State;                            // ATIM 刹车输入2使能状态控制
    ATIM_Break2PolarityTypeDef          Break2Polarity;                         // ATIM 刹车输入2极性控制
    uint32_t                            Break2Filter;                           // ATIM 刹车输入2滤波参数 0x0~0xF
    ATIM_AutomaticOutputTypeDef         AutomaticOutput;                        // ATIM 自动输出使能状态控制
} ATIM_BreakDeadTimeConfigTypeDef;

/* ATIM 霍尔传感器配置结构体 ---------------------------------------------------*/
typedef struct
{
    ATIM_ICPolarityTypeDef              IC1Polarity;                            // 输入捕获信号极性
    ATIM_ICPrescalerTypeDef             IC1Prescaler;                           // 输入捕获信号预分频
    uint32_t                            IC1Filter;                              // 输入捕获信号滤波参数
    uint32_t                            Commutation_Delay;                      // 脉冲宽度 向捕获比较寄存器装载的值 0x0000~0xFFFF
} ATIM_HallSensor_InitTypeDef;

/* ATIM Break/Break2 刹车输入配置结构体 ---------------------------------------*/
typedef struct
{
    ATIM_BreakSourceTypeDef             Source;                                 // 刹车输入源配置
    ATIM_BreakSourceEnTypeDef           Enable;                                 // 刹车输入源使能控制
    ATIM_BreakSourcePolarityTypeDef     Polarity;                               // 刹车输入极性控制
} ATIM_BreakInputConfigTypeDef;

/* ATIM 编码器索引信号配置结构体 -----------------------------------------------*/
typedef struct
{
    ATIM_IndexPolarityTypeDef           Polarity;                               // ATIM 编码器索引信号极性配置
    ATIM_IndexPrescalerTypeDef          Prescaler;                              // ATIM 编码器索引信号预分频配置
    uint32_t                            Filter;                                 // ATIM 编码器索引信号滤波参数
    FunctionalState                     FirstIndexEnable;                       // ATIM 编码器是否只执行首次索引信号,忽略后续索引信号
    ATIM_IndexPositionTypeDef           Position;                               // ATIM 编码器索引位置配置
    ATIM_IndexDirectionTypeDef          Direction;                              // ATIM 编码器索引信号有效计数方向配置
} ATIM_EncoderIndexConfigTypeDef;

 /* ATIM 初始化配置句柄类型 ---------------------------------------------------*/
typedef struct
{
    ATIM_TypeDef                        *Instance;                              // ATIM 基地址
    ATIM_Base_InitTypeDef               Init;                                   // ATIM 时基初始化
    ATIM_OC_InitTypeDef                 OcInit;                                 // ATIM 输出比较初始化
    ATIM_IC_InitTypeDef                 IcInit;                                 // ATIM 输入捕获初始化
    HAL_ATIM_ActiveChannel              Channel;                                // 活跃通道
} ATIM_HandleTypeDef;


/******************************************************************************/
/* Global pre-processor symbols/macros ('#define')                            */
/******************************************************************************/
/** @defgroup ATIM_Channel ATIM Channel ***************************************/
#define ATIM_CHANNEL_1                  0x00000001UL                            // 捕获/比较通道1
#define ATIM_CHANNEL_2                  0x00000010UL                            // 捕获/比较通道2
#define ATIM_CHANNEL_3                  0x00000100UL                            // 捕获/比较通道3
#define ATIM_CHANNEL_4                  0x00001000UL                            // 捕获/比较通道4
#define ATIM_CHANNEL_5                  0x00010000UL                            // 捕获/比较通道5
#define ATIM_CHANNEL_6                  0x00100000UL                            // 捕获/比较通道6
#define ATIM_CHANNEL_ALL                0x00111111UL                            // 所有捕获/比较通道

/*********** ATIM Instances : Slave mode available (TIMx_SMCR available )*******/
#define ATIM_CR2_MMS                    (ATIM_CR2_MMS_Msk | ATIM_CR2_MMSH_Msk)
#define ATIM_SMCR_TS                    (ATIM_SMCR_TS_Msk  | ATIM_SMCR_TSH_Msk)
#define ATIM_SMCR_SMS                   (ATIM_SMCR_SMS_Msk | ATIM_SMCR_SMSH_Msk)
#define ATIM_CCMR1CMP_OC1M              (ATIM_CCMR1CMP_OC1M_Msk | ATIM_CCMR1CMP_OC1MH_Msk)
#define ATIM_CCMR1CMP_OC2M              (ATIM_CCMR1CMP_OC2M_Msk | ATIM_CCMR1CMP_OC2MH_Msk)
#define ATIM_CCMR2CMP_OC3M              (ATIM_CCMR2CMP_OC3M_Msk | ATIM_CCMR2CMP_OC3MH_Msk)
#define ATIM_CCMR2CMP_OC4M              (ATIM_CCMR2CMP_OC4M_Msk | ATIM_CCMR2CMP_OC4MH_Msk)
#define ATIM_CCMR3CMP_OC5M              (ATIM_CCMR3CMP_OC5M_Msk | ATIM_CCMR3CMP_OC5MH_Msk)
#define ATIM_CCMR3CMP_OC6M              (ATIM_CCMR3CMP_OC6M_Msk | ATIM_CCMR3CMP_OC6MH_Msk)

#define ATIM_CCER_CCxE_MASK  ((uint32_t)(ATIM_CCER_CC1E_Msk | ATIM_CCER_CC2E_Msk | \
                                        ATIM_CCER_CC3E_Msk | ATIM_CCER_CC4E_Msk | \
                                        ATIM_CCER_CC5E_Msk | ATIM_CCER_CC6E_Msk))

#define ATIM_CCER_CCxNE_MASK ((uint32_t)(ATIM_CCER_CC1NE_Msk | ATIM_CCER_CC2NE_Msk | \
                                        ATIM_CCER_CC3NE_Msk | ATIM_CCER_CC4NE_Msk | \
                                        ATIM_CCER_CC5NE_Msk | ATIM_CCER_CC6NE_Msk))

/** @defgroup ATIM_Event_Source ATIM Event Source */
#define ATIM_EVENTSOURCE_UPDATE         ATIM_EGR_UG_Msk                         // 重新初始化计数器并产生一个更新事件更新寄存器
#define ATIM_EVENTSOURCE_CC1            ATIM_EGR_CC1G_Msk                       // 在CH1上产生一个 捕获/比较 事件
#define ATIM_EVENTSOURCE_CC2            ATIM_EGR_CC2G_Msk                       // 在CH2上产生一个 捕获/比较 事件
#define ATIM_EVENTSOURCE_CC3            ATIM_EGR_CC3G_Msk                       // 在CH3上产生一个 捕获/比较 事件
#define ATIM_EVENTSOURCE_CC4            ATIM_EGR_CC4G_Msk                       // 在CH4上产生一个 捕获/比较 事件
#define ATIM_EVENTSOURCE_CC5            ATIM_EGR_CC5G_Msk                       // 在CH5上产生一个 捕获/比较 事件
#define ATIM_EVENTSOURCE_CC6            ATIM_EGR_CC6G_Msk                       // 在CH6上产生一个 捕获/比较 事件
#define ATIM_EVENTSOURCE_COM            ATIM_EGR_COMG_Msk                       // 产生一个 COM 事件
#define ATIM_EVENTSOURCE_TRIGGER        ATIM_EGR_TG_Msk                         // 产生一个 Trigger 事件
#define ATIM_EVENTSOURCE_BREAK          ATIM_EGR_BG_Msk                         // 产生一个 break 事件
#define ATIM_EVENTSOURCE_BREAK2         ATIM_EGR_B2G_Msk                        // 产生一个 break2 事件

/** @defgroup ATIM_Update_Interrupt_Flag_Remap ATIM Update Interrupt Flag Remap */
#define ATIM_UIFREMAP_DISABLE           0x00000000UL                            // 禁止更新中断标志重映射
#define ATIM_UIFREMAP_ENABLE            ATIM_CR1_UIFREMAP_Msk                   // 使能更新中断标志重映射

/** @defgroup ATIM_Output_Compare_State ATIM Output Compare State */
#define ATIM_OUTPUTSTATE_DISABLE        0x00000000UL                            // ATIM输出比较状态 Disable
#define ATIM_OUTPUTSTATE_ENABLE         ATIM_CCER_CC1E_Msk                      // ATIM输出比较状态 Enable

/** @defgroup ATIM_Output_Compare_N_State ATIM Complementary Output Compare State */
#define ATIM_OUTPUTNSTATE_DISABLE       0x00000000UL                            // ATIM互补输出比较状态 Disable
#define ATIM_OUTPUTNSTATE_ENABLE        ATIM_CCER_CC1NE_Msk                     // ATIM互补输出比较状态 Enable

/** @defgroup ATIM_One_Pulse_Mode ATIM One Pulse Mode */
#define ATIM_OPMODE_SINGLE              ATIM_CR1_OPM_Msk                        // 在下次发生更新事件时, 计数器停止计数
#define ATIM_OPMODE_REPETITIVE          0x00000000UL                            // 在下次发生更新事件时, 计数器不停止

/** @defgroup ATIM_Interrupt_definition ATIM interrupt Definition */
#define ATIM_IT_SOURCE_UPDATE           ATIM_DIER_UIE_Msk                       // ATIM 更新中断 (Update/UIE)
#define ATIM_IT_SOURCE_CC1              ATIM_DIER_CC1IE_Msk                     // ATIM 捕获/比较1 中断
#define ATIM_IT_SOURCE_CC2              ATIM_DIER_CC2IE_Msk                     // ATIM 捕获/比较2 中断
#define ATIM_IT_SOURCE_CC3              ATIM_DIER_CC3IE_Msk                     // ATIM 捕获/比较3 中断
#define ATIM_IT_SOURCE_CC4              ATIM_DIER_CC4IE_Msk                     // ATIM 捕获/比较4 中断
#define ATIM_IT_SOURCE_CC5              ATIM_DIER_CC5IE_Msk                     // ATIM 捕获/比较5 中断
#define ATIM_IT_SOURCE_CC6              ATIM_DIER_CC6IE_Msk                     // ATIM 捕获/比较6 中断
#define ATIM_IT_SOURCE_COM              ATIM_DIER_COMIE_Msk                     // ATIM COM 中断
#define ATIM_IT_SOURCE_TRIGGER          ATIM_DIER_TIE_Msk                       // ATIM Trigger 中断
#define ATIM_IT_SOURCE_BREAK            ATIM_DIER_BIE_Msk                       // ATIM Break 中断
#define ATIM_IT_SOURCE_IDX              ATIM_DIER_IDXIE_Msk                     // ATIM Index 中断
#define ATIM_IT_SOURCE_DIR              ATIM_DIER_DIRIE_Msk                     // ATIM 方向改变 中断
#define ATIM_IT_SOURCE_IERR             ATIM_DIER_IERRIE_Msk                    // ATIM 编码器错误 中断
#define ATIM_IT_SOURCE_TERR             ATIM_DIER_TERRIE_Msk                    // ATIM 正交编码转换 错误

/** @defgroup ATIM_Commutation_Source  ATIM Commutation Source  */
#define ATIM_COMMUTATION_TRGI           ATIM_CR2_CCUS_Msk                       // 当使能了捕获比较控制位的预装载功能, 在向COMG写1或Trigger检测到上升沿时更新其影子寄存器(CCxE CCxNE OCxM)
#define ATIM_COMMUTATION_SOFTWARE       0x00000000UL                            // 当使能了捕获比较控制位的预装载功能, 仅向COMG写1时更新其影子寄存器(CCxE CCxNE OCxM)

/** @defgroup ATIM_IT_Flag_definition ATIM Flag Definition */
#define ATIM_IT_FLAG_UPDATE             ATIM_ISR_UIF_Msk                        // 更新事件 中断标志
#define ATIM_IT_FLAG_CC1                ATIM_ISR_CC1IF_Msk                      // 捕获/比较1 中断标志
#define ATIM_IT_FLAG_CC2                ATIM_ISR_CC2IF_Msk                      // 捕获/比较2 中断标志
#define ATIM_IT_FLAG_CC3                ATIM_ISR_CC3IF_Msk                      // 捕获/比较3 中断标志
#define ATIM_IT_FLAG_CC4                ATIM_ISR_CC4IF_Msk                      // 捕获/比较4 中断标志
#define ATIM_IT_FLAG_CC5                ATIM_ISR_CC5IF_Msk                      // 捕获/比较5 中断标志
#define ATIM_IT_FLAG_CC6                ATIM_ISR_CC6IF_Msk                      // 捕获/比较6 中断标志
#define ATIM_IT_FLAG_COM                ATIM_ISR_COMIF_Msk                      // COM 中断标志
#define ATIM_IT_FLAG_TRIGGER            ATIM_ISR_TIF_Msk                        // Trigger 触发中断标志
#define ATIM_IT_FLAG_BREAK              ATIM_ISR_BIF_Msk                        // Break 刹车中断标志
#define ATIM_IT_FLAG_BREAK2             ATIM_ISR_B2IF_Msk                       // break2 刹车2中断标志
#define ATIM_IT_FLAG_SYSTEM_BREAK       ATIM_ISR_SBIF_Msk                       // 系统刹车 中断标志
#define ATIM_IT_FLAG_IDX                ATIM_ISR_IDXF_Msk                       // 编码计数索引 中断标志
#define ATIM_IT_FLAG_DIR                ATIM_ISR_DIRF_Msk                       // 编码计数方向改变 中断标志
#define ATIM_IT_FLAG_IERR               ATIM_ISR_IERRF_Msk                      // 编码计数索引错误 中断标志
#define ATIM_IT_FLAG_TERR               ATIM_ISR_TERRF_Msk                      // 正交编码转换错误 中断标志

/** @defgroup ATIM_Flag_definition ATIM Flag Definition */
#define ATIM_FLAG_UPDATE                ATIM_ISR_UIF_Msk                        // 更新事件 标志
#define ATIM_FLAG_CC1                   ATIM_ISR_CC1IF_Msk                      // 捕获/比较1 标志
#define ATIM_FLAG_CC2                   ATIM_ISR_CC2IF_Msk                      // 捕获/比较2 标志
#define ATIM_FLAG_CC3                   ATIM_ISR_CC3IF_Msk                      // 捕获/比较3 标志
#define ATIM_FLAG_CC4                   ATIM_ISR_CC4IF_Msk                      // 捕获/比较4 标志
#define ATIM_FLAG_CC5                   ATIM_ISR_CC5IF_Msk                      // 捕获/比较5 标志
#define ATIM_FLAG_CC6                   ATIM_ISR_CC6IF_Msk                      // 捕获/比较6 标志
#define ATIM_FLAG_COM                   ATIM_ISR_COMIF_Msk                      // COM 标志
#define ATIM_FLAG_TRIGGER               ATIM_ISR_TIF_Msk                        // Trigger 触发标志
#define ATIM_FLAG_BREAK                 ATIM_ISR_BIF_Msk                        // Break 刹车标志
#define ATIM_FLAG_BREAK2                ATIM_ISR_B2IF_Msk                       // break2 刹车2标志
#define ATIM_FLAG_SYSTEM_BREAK          ATIM_ISR_SBIF_Msk                       // 系统刹车 标志
#define ATIM_FLAG_IDX                   ATIM_ISR_IDXF_Msk                       // 编码计数索引 标志
#define ATIM_FLAG_DIR                   ATIM_ISR_DIRF_Msk                       // 编码计数方向改变 标志
#define ATIM_FLAG_IERR                  ATIM_ISR_IERRF_Msk                      // 编码计数索引错误 标志
#define ATIM_FLAG_TERR                  ATIM_ISR_TERRF_Msk                      // 正交编码转换错误 标志
#define ATIM_FLAG_CC1OF                 ATIM_ISR_CC1OF_Msk                      // 捕获1 重复捕获 标志
#define ATIM_FLAG_CC2OF                 ATIM_ISR_CC2OF_Msk                      // 捕获2 重复捕获 标志
#define ATIM_FLAG_CC3OF                 ATIM_ISR_CC3OF_Msk                      // 捕获3 重复捕获 标志
#define ATIM_FLAG_CC4OF                 ATIM_ISR_CC4OF_Msk                      // 捕获4 重复捕获 标志
#define ATIM_FLAG_CC5OF                 ATIM_ISR_CC5OF_Msk                      // 捕获5 重复捕获 标志
#define ATIM_FLAG_CC6OF                 ATIM_ISR_CC6OF_Msk                      // 捕获6 重复捕获 标志

/** @defgroup ATIM_Group_Channel5 Group Channel 6 and Channel 1, 2, 3, 4, 5 or 6 */
#define ATIM_GROUPCH5_NONE              0x00000000UL                            // OC5REF 对其他通道无影响
#define ATIM_GROUPCH5_OC1REFC           ATIM_CCR5_GC5C1_Msk                     // OC1REFC = OC1REF & OC5REF
#define ATIM_GROUPCH5_OC2REFC           ATIM_CCR5_GC5C2_Msk                     // OC2REFC = OC2REF & OC5REF
#define ATIM_GROUPCH5_OC3REFC           ATIM_CCR5_GC5C3_Msk                     // OC3REFC = OC3REF & OC5REF
#define ATIM_GROUPCH5_OC4REFC           ATIM_CCR5_GC5C4_Msk                     // OC4REFC = OC4REF & OC5REF
#define ATIM_GROUPCH5_OC5REFC           ATIM_CCR5_GC5C5_Msk                     // OC5REFC = OC5REF & OC5REF
#define ATIM_GROUPCH5_OC6REFC           ATIM_CCR5_GC5C6_Msk                     // OC6REFC = OC6REF & OC5REF

/** @defgroup ATIM_Group_Channel6 Group Channel 6 and Channel 1, 2, 3, 4, 5 or 6 */
#define ATIM_GROUPCH6_NONE              0x00000000UL                            // OC6REF 对其他通道无影响
#define ATIM_GROUPCH6_OC1REFC           ATIM_CCR6_GC6C1_Msk                     // 递增计数时 OC1REFC = OC1REF, 递减计数时 OC1REFC = OC6REF
#define ATIM_GROUPCH6_OC2REFC           ATIM_CCR6_GC6C2_Msk                     // 递增计数时 OC2REFC = OC2REF, 递减计数时 OC2REFC = OC6REF
#define ATIM_GROUPCH6_OC3REFC           ATIM_CCR6_GC6C3_Msk                     // 递增计数时 OC3REFC = OC3REF, 递减计数时 OC3REFC = OC6REF
#define ATIM_GROUPCH6_OC4REFC           ATIM_CCR6_GC6C4_Msk                     // 递增计数时 OC4REFC = OC4REF, 递减计数时 OC4REFC = OC6REF
#define ATIM_GROUPCH6_OC5REFC           ATIM_CCR6_GC6C5_Msk                     // 递增计数时 OC5REFC = OC5REF, 递减计数时 OC5REFC = OC6REF
#define ATIM_GROUPCH6_OC6REFC           ATIM_CCR6_GC6C6_Msk                     // 递增计数时 OC6REFC = OC6REF, 递减计数时 OC6REFC = OC6REF

/** @defgroup ATIM_TI1_Selection ATIM TI1 Input Selection */
#define ATIM_TI1SELECTION_CH1            0x00000000UL                           // TI1 = ATIM_CH1 管脚
#define ATIM_TI1SELECTION_XORCOMBINATION ATIM_CR2_TI1S_Msk                      // TI1 = ATIM_CH1 ^ ATIM_CH2 ^ ATIM_CH1 (三通道异或)

/** @defgroup Channel_CC_State ATIM Capture/Compare Channel State */
#define ATIM_CCx_ENABLE                 0x00000001UL                            // CC1输入或输出通道 使能
#define ATIM_CCx_DISABLE                0x00000000UL                            // CC1输入或输出通道 禁止
#define ATIM_CCxN_ENABLE                0x00000004UL                            // CC1N互补输出通道 使能
#define ATIM_CCxN_DISABLE               0x00000000UL                            // CC1N互补输出通道 禁止

/** @defgroup ATIM_Break_System ATIM Break System */
#define ATIM_BREAK_SYSTEM_SRAM_PARITY_ERROR SYSCTRL_CR2_RAMBRKEN_Msk            // RAM奇偶校验错误标志触发ATIM刹车使能控制
#define ATIM_BREAK_SYSTEM_LVD               SYSCTRL_CR2_LVDBRKEN_Msk            // LVD_OUT状态触发ATIM刹车使能控制
#define ATIM_BREAK_SYSTEM_DEEPSLEEP         SYSCTRL_CR2_DSBRKEN_Msk             // 进入DeepSleep状态触发ATIM刹车使能控制
#define ATIM_BREAK_SYSTEM_LOCKUP            SYSCTRL_CR2_CLLBRKEN_Msk            // 进入HardFault或LockUp状态触发ATIM刹车使能控制
#define ATIM_BREAK_SYSTEM_LSE               SYSCTRL_CR2_LSEBRKEN_Msk            // LSE运行失效标志触发ATIM刹车使能控制
#define ATIM_BREAK_SYSTEM_HSE               SYSCTRL_CR2_HSEBRKEN_Msk            // HSE运行失效标志触发ATIM刹车使能控制

/** @defgroup ATIM_Remap ATIM Extended Remapping */
#define ATIM_ETRSEL_GPIO                0x00000000UL                            // ETR 管脚 作为ETR信号来源
#define ATIM_ETRSEL_VC1_OUT             (0x1 << ATIM_AF1_ETRSEL_Pos)            // VC1_OUT 作为ETR信号来源
#define ATIM_ETRSEL_VC2_OUT             (0x2 << ATIM_AF1_ETRSEL_Pos)            // VC2_OUT 作为ETR信号来源
#define ATIM_ETRSEL_ADC_AWD             (0x5 << ATIM_AF1_ETRSEL_Pos)            // ADC_AWD 作为ETR信号来源
#define ATIM_ETRSEL_HSE_FAULT           (0x7 << ATIM_AF1_ETRSEL_Pos)            // HSE_FAULT 作为ETR信号来源
#define ATIM_ETRSEL_LVD_OUT             (0x8 << ATIM_AF1_ETRSEL_Pos)            // LVD_OUT 作为ETR信号来源
#define ATIM_ETRSEL_GTIM1_ETR           (0x9 << ATIM_AF1_ETRSEL_Pos)            // GTIM1_ETR 作为ETR信号来源
#define ATIM_ETRSEL_UART1_TXD           (0xD << ATIM_AF1_ETRSEL_Pos)            // UART1_TXD 作为ETR信号来源
#define ATIM_ETRSEL_UART2_TXD           (0xE << ATIM_AF1_ETRSEL_Pos)            // UART2_TXD 作为ETR信号来源
#define ATIM_ETRSEL_LSE_FAULT           (0xF << ATIM_AF1_ETRSEL_Pos)            // LSE_FAULT 作为ETR信号来源

/** @defgroup ATIM_Break_Input ATIM Extended Break input */
#define ATIM_BREAKINPUT_BRK             0x00000001UL                            // break 输入
#define ATIM_BREAKINPUT_BRK2            0x00000002UL                            // break2 输入

/** @defgroup ATIM_Timer_Input_Selection ATIM Extended Timer input selection */
#define ATIM_TIxSEL_GPIO                0x00000000UL                            // GPIO 作为CHx输入捕获来源
#define ATIM_TIxSEL_VC1_OUT             (0x1 << ATIM_TISEL1_TI1SEL_Pos)         // VC1_OUT 作为CHx输入捕获来源
#define ATIM_TIxSEL_VC2_OUT             (0x2 << ATIM_TISEL1_TI1SEL_Pos)         // VC2_OUT 作为CHx输入捕获来源
#define ATIM_TIxSEL_UART1_RXD           (0x3 << ATIM_TISEL1_TI1SEL_Pos)         // UART1_RXD作为CHx输入捕获来源
#define ATIM_TIxSEL_UART2_RXD           (0x4 << ATIM_TISEL1_TI1SEL_Pos)         // UART2_RXD作为CHx输入捕获来源
#define ATIM_TIxSEL_MCO_OUT             (0x5 << ATIM_TISEL1_TI1SEL_Pos)         // MCO_OUT 作为CHx输入捕获来源
#define ATIM_TIxSEL_HSE_FAULT           (0x6 << ATIM_TISEL1_TI1SEL_Pos)         // HSE_FAULT 作为CHx输入捕获来源
#define ATIM_TIxSEL_LSE_FAULT           (0x7 << ATIM_TISEL1_TI1SEL_Pos)         // LSE_FAULT 作为CHx输入捕获来源
#define ATIM_TIxSEL_RTC_OUT             (0x8 << ATIM_TISEL1_TI1SEL_Pos)         // RTC_OUT 作为CHx输入捕获来源
#define ATIM_TIxSEL_LSI_OUT             (0x9 << ATIM_TISEL1_TI1SEL_Pos)         // LSI_OUT 作为CHx输入捕获来源
#define ATIM_TIxSEL_BTIM1_TRGO          (0xA << ATIM_TISEL1_TI1SEL_Pos)         // BTIM1_Trgo 作为CHx输入捕获来源
#define ATIM_TIxSEL_BTIM2_TRGO          (0xB << ATIM_TISEL1_TI1SEL_Pos)         // BTIM2_Trgo 作为CHx输入捕获来源
#define ATIM_TIxSEL_BTIM3_TRGO          (0xC << ATIM_TISEL1_TI1SEL_Pos)         // BTIM3_Trgo 作为CHx输入捕获来源
#define ATIM_TIxSEL_GTIM1_TRGO          (0xD << ATIM_TISEL1_TI1SEL_Pos)         // GTIM1_Trgo 作为CHx输入捕获来源

/** @defgroup ATIM_SMS_Preload_Enable ATIM Extended Bitfield SMS preload enabling */
#define ATIM_SMS_PRELOAD_SOURCE_UPDATE  0x00000000UL                            // 发生更新事件时更新SMS的影子寄存器
#define ATIM_SMS_PRELOAD_SOURCE_INDEX   ATIM_SMCR_SMSPS_Msk                     // 发生索引事件时更新SMS的影子寄存器


/******************************************************************************/
/* Global macro function                                                      */
/******************************************************************************/
/**
  * @brief  使能 ATIM 外设
  * @param  __HANDLE__ ATIM 句柄指针
  * @retval None
  */
#define HAL_ATIM_ENABLE(__HANDLE__)    ((__HANDLE__)->Instance->CR1 |= (ATIM_CR1_CEN_Msk))

/**
  * @brief  ATIM 主输出使能
  * @param  __HANDLE__ ATIM 句柄指针
  * @retval None
  */
#define HAL_ATIM_MOE_ENABLE(__HANDLE__)    ((__HANDLE__)->Instance->BDTR |= (ATIM_BDTR_MOE_Msk))

/**
  * @brief  禁止 ATIM 外设
  * @param  __HANDLE__ ATIM 句柄指针
  * @retval None
  */
#define HAL_ATIM_DISABLE(__HANDLE__)                                         \
    do {                                                                     \
        if (((__HANDLE__)->Instance->CCER & ATIM_CCER_CCxE_MASK) == 0UL)     \
        {                                                                    \
            if(((__HANDLE__)->Instance->CCER & ATIM_CCER_CCxNE_MASK) == 0UL) \
            {                                                                \
                (__HANDLE__)->Instance->CR1 &= ~(ATIM_CR1_CEN_Msk);          \
            }                                                                \
       }                                                                     \
    } while (0)

/**
  * @brief  禁止 ATIM 主输出
  * @param  __HANDLE__ ATIM 句柄指针
  * @retval None
  * @note   只有当所有的CCx和CCxN通道都被禁用时，定时器实例的主输出才被禁用
  */
#define HAL_ATIM_MOE_DISABLE(__HANDLE__)                                     \
    do {                                                                     \
        if (((__HANDLE__)->Instance->CCER & ATIM_CCER_CCxE_MASK) == 0UL)     \
        {                                                                    \
            if(((__HANDLE__)->Instance->CCER & ATIM_CCER_CCxNE_MASK) == 0UL) \
            {                                                                \
                (__HANDLE__)->Instance->BDTR &= ~(ATIM_BDTR_MOE_Msk);        \
            }                                                                \
        }                                                                    \
  } while (0)

/**
  * @brief  禁止 ATIM 主输出
  * @param  __HANDLE__ ATIM 句柄指针
  * @retval None
  * @note   无条件禁用 ATIM 主输出
  */
#define HAL_ATIM_MOE_DISABLE_UNCONDITIONALLY(__HANDLE__)    (__HANDLE__)->Instance->BDTR &= ~(ATIM_BDTR_MOE_Msk)

/**
  * @brief  检查指定的 ATIM 中断源是否被使能
  * @param  __HANDLE__    AIM handle
  * @param  __INTERRUPT__ 指定要检查的 ATIM 中断源，该参数可为下列之一:
  *             @arg ATIM_IT_SOURCE_UPDATE:  更新中断 (Update/UIE)
  *             @arg ATIM_IT_SOURCE_CC1:     捕获/比较1 中断
  *             @arg ATIM_IT_SOURCE_CC2:     捕获/比较2 中断
  *             @arg ATIM_IT_SOURCE_CC3:     捕获/比较3 中断
  *             @arg ATIM_IT_SOURCE_CC4:     捕获/比较4 中断
  *             @arg ATIM_IT_SOURCE_CC5:     捕获/比较5 中断
  *             @arg ATIM_IT_SOURCE_CC6:     捕获/比较6 中断
  *             @arg ATIM_IT_SOURCE_COM:     COM 中断
  *             @arg ATIM_IT_SOURCE_TRIGGER: Trigger 中断
  *             @arg ATIM_IT_SOURCE_BREAK:   Break 中断
  *             @arg ATIM_IT_SOURCE_IDX:     Index 中断
  *             @arg ATIM_IT_SOURCE_DIR:     方向改变 中断
  *             @arg ATIM_IT_SOURCE_IERR:    编码器计数索引错误 中断
  *             @arg ATIM_IT_SOURCE_TERR:    正交编码转换错误 中断
  * @retval The state of ATIM_IT_SOURCE (SET or RESET).
  */
#define HAL_ATIM_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__)    ((((__HANDLE__)->Instance->DIER & (__INTERRUPT__)) \
                                                              == (__INTERRUPT__)) ? SET : RESET)

/**
  * @brief  使能指定的 ATIM 中断
  * @param  __HANDLE__    ATIM 句柄指针
  * @param  __INTERRUPT__ 指定要使能的 ATIM 中断源，该参数可为下列之一:
  *             @arg ATIM_IT_SOURCE_UPDATE:  更新中断 (Update/UIE)
  *             @arg ATIM_IT_SOURCE_CC1:     捕获/比较1 中断
  *             @arg ATIM_IT_SOURCE_CC2:     捕获/比较2 中断
  *             @arg ATIM_IT_SOURCE_CC3:     捕获/比较3 中断
  *             @arg ATIM_IT_SOURCE_CC4:     捕获/比较4 中断
  *             @arg ATIM_IT_SOURCE_CC5:     捕获/比较5 中断
  *             @arg ATIM_IT_SOURCE_CC6:     捕获/比较6 中断
  *             @arg ATIM_IT_SOURCE_COM:     COM 中断
  *             @arg ATIM_IT_SOURCE_TRIGGER: Trigger 中断
  *             @arg ATIM_IT_SOURCE_BREAK:   Break 中断
  *             @arg ATIM_IT_SOURCE_IDX:     Index 中断
  *             @arg ATIM_IT_SOURCE_DIR:     方向改变 中断
  *             @arg ATIM_IT_SOURCE_IERR:    编码器计数索引错误 中断
  *             @arg ATIM_IT_SOURCE_TERR:    正交编码转换错误 中断
  * @retval None
  */
#define HAL_ATIM_ENABLE_IT(__HANDLE__, __INTERRUPT__)    ((__HANDLE__)->Instance->DIER |= (__INTERRUPT__))

/**
  * @brief  禁止指定的 ATIM 中断源
  * @param  __HANDLE__    ATIM 句柄指针
  * @param  __INTERRUPT__ 指定要禁止的 ATIM 中断源，该参数可为下列之一:
  *             @arg ATIM_IT_SOURCE_UPDATE:  更新中断 (Update/UIE)
  *             @arg ATIM_IT_SOURCE_CC1:     捕获/比较1 中断
  *             @arg ATIM_IT_SOURCE_CC2:     捕获/比较2 中断
  *             @arg ATIM_IT_SOURCE_CC3:     捕获/比较3 中断
  *             @arg ATIM_IT_SOURCE_CC4:     捕获/比较4 中断
  *             @arg ATIM_IT_SOURCE_CC5:     捕获/比较5 中断
  *             @arg ATIM_IT_SOURCE_CC6:     捕获/比较6 中断
  *             @arg ATIM_IT_SOURCE_COM:     COM 中断
  *             @arg ATIM_IT_SOURCE_TRIGGER: Trigger 中断
  *             @arg ATIM_IT_SOURCE_BREAK:   Break 中断
  *             @arg ATIM_IT_SOURCE_IDX:     Index 中断
  *             @arg ATIM_IT_SOURCE_DIR:     方向改变 中断
  *             @arg ATIM_IT_SOURCE_IERR:    编码器计数索引错误 中断
  *             @arg ATIM_IT_SOURCE_TERR:    正交编码转换错误 中断
  * @retval None
  */
#define HAL_ATIM_DISABLE_IT(__HANDLE__, __INTERRUPT__)    ((__HANDLE__)->Instance->DIER &= ~(__INTERRUPT__))
#define HAL_ATIM_CLEAR_IT  HAL_ATIM_DISABLE_IT

/**
  * @brief  检查指定的 ATIM 中断标志
  * @param  __HANDLE__ ATIM 句柄指针
  * @param  __FLAG__   指定要检查的 ATIM 中断标志，该参数可为下列之一:
  *             @arg ATIM_FLAG_UPDATE:       更新事件 标志
  *             @arg ATIM_FLAG_CC1:          捕获/比较1 标志
  *             @arg ATIM_FLAG_CC2:          捕获/比较2 标志
  *             @arg ATIM_FLAG_CC3:          捕获/比较3 标志
  *             @arg ATIM_FLAG_CC4:          捕获/比较4 标志
  *             @arg ATIM_FLAG_CC5:          捕获/比较5 标志
  *             @arg ATIM_FLAG_CC6:          捕获/比较6 标志
  *             @arg ATIM_FLAG_COM:          COM 标志
  *             @arg ATIM_FLAG_TRIGGER:      Trigger 触发标志
  *             @arg ATIM_FLAG_BREAK:        Break 刹车标志
  *             @arg ATIM_FLAG_BREAK2:       break2 刹车2标志
  *             @arg ATIM_FLAG_SYSTEM_BREAK: 系统刹车 标志
  *             @arg ATIM_FLAG_CC1OF:        捕获1 重复捕获 标志
  *             @arg ATIM_FLAG_CC2OF:        捕获2 重复捕获 标志
  *             @arg ATIM_FLAG_CC3OF:        捕获3 重复捕获 标志
  *             @arg ATIM_FLAG_CC4OF:        捕获4 重复捕获 标志
  *             @arg ATIM_FLAG_CC5OF:        捕获5 重复捕获 标志
  *             @arg ATIM_FLAG_CC6OF:        捕获6 重复捕获 标志
  *             @arg ATIM_FLAG_IDX:          编码计数索引 标志
  *             @arg ATIM_FLAG_DIR:          编码计数方向改变 标志
  *             @arg ATIM_FLAG_IERR:         编码计数索引错误 标志
  *             @arg ATIM_FLAG_TERR:         正交编码转换错误 标志
  * @retval The new state of __FLAG__ (TRUE or FALSE).
  */
#define HAL_ATIM_GET_FLAG(__HANDLE__, __FLAG__)    (((__HANDLE__)->Instance->ISR &(__FLAG__)) == (__FLAG__))

/**
  * @brief  清除指定的 ATIM 中断标志
  * @param  __HANDLE__ ATIM 句柄指针
  * @param  __FLAG__   指定要清除的 ATIM 中断标志，该参数可为下列之一:
  *             @arg ATIM_FLAG_UPDATE:       更新事件 标志
  *             @arg ATIM_FLAG_CC1:          捕获/比较1 标志
  *             @arg ATIM_FLAG_CC2:          捕获/比较2 标志
  *             @arg ATIM_FLAG_CC3:          捕获/比较3 标志
  *             @arg ATIM_FLAG_CC4:          捕获/比较4 标志
  *             @arg ATIM_FLAG_CC5:          捕获/比较5 标志
  *             @arg ATIM_FLAG_CC6:          捕获/比较6 标志
  *             @arg ATIM_FLAG_COM:          COM 标志
  *             @arg ATIM_FLAG_TRIGGER:      Trigger 触发标志
  *             @arg ATIM_FLAG_BREAK:        Break 刹车标志
  *             @arg ATIM_FLAG_BREAK2:       break2 刹车2标志
  *             @arg ATIM_FLAG_SYSTEM_BREAK: 系统刹车 标志
  *             @arg ATIM_FLAG_CC1OF:        捕获1 重复捕获 标志
  *             @arg ATIM_FLAG_CC2OF:        捕获2 重复捕获 标志
  *             @arg ATIM_FLAG_CC3OF:        捕获3 重复捕获 标志
  *             @arg ATIM_FLAG_CC4OF:        捕获4 重复捕获 标志
  *             @arg ATIM_FLAG_CC5OF:        捕获5 重复捕获 标志
  *             @arg ATIM_FLAG_CC6OF:        捕获6 重复捕获 标志
  *             @arg ATIM_FLAG_IDX:          编码计数索引 标志
  *             @arg ATIM_FLAG_DIR:          编码计数方向改变 标志
  *             @arg ATIM_FLAG_IERR:         编码计数索引错误 标志
  *             @arg ATIM_FLAG_TERR:         正交编码转换错误 标志
  * @retval The new state of __FLAG__ (TRUE or FALSE)
  */
#define HAL_ATIM_CLEAR_FLAG(__HANDLE__, __FLAG__)    ((__HANDLE__)->Instance->ICR = ~(__FLAG__))
#define HAL_ATIM_CLR_FLAG  HAL_ATIM_CLEAR_FLAG

/**
  * @brief  强制将更新中断标志(UIF)连续复制到定时器计数器寄存器(位31)
  * @note   用户通过一次读操作即可同时获取计数值CNT以及更新标志ISR.UIF
  * @param  __HANDLE__ ATIM 句柄指针
  * @retval None
  */
#define HAL_ATIM_UIFREMAP_ENABLE(__HANDLE__)    (((__HANDLE__)->Instance->CR1 |= ATIM_CR1_UIFREMAP_Msk))

/**
  * @brief  禁用更新中断标志(UIF)重映射
  * @param  __HANDLE__ ATIM 句柄指针
  * @retval None
  */
#define HAL_ATIM_UIFREMAP_DISABLE(__HANDLE__)    (((__HANDLE__)->Instance->CR1 &= ~ATIM_CR1_UIFREMAP_Msk))

/**
  * @brief  获取UIFCPY的状态
  * @param  __COUNTER__ ATIM_CNT 寄存器值
  * @retval UIFCPY的状态 (TRUE or FALSE)
  */
#define HAL_ATIM_GET_UIFCPY(__COUNTER__)    (((__COUNTER__) & (ATIM_CNT_UIFCPY_Msk)) == (ATIM_CNT_UIFCPY_Msk))

/**
  * @brief  指示当前是否将ATIM计数器用作downcounter
  * @param  __HANDLE__ ATIM 句柄指针
  * @retval False (Counter 加计数) 或 True (Counter 减计数)
  * @note   当定时器以中心对齐模式或编码器工作时, 这个宏对于获取计数方向特别有用
  */
#define HAL_ATIM_IS_TIM_COUNTING_DOWN(__HANDLE__)    (((__HANDLE__)->Instance->CR1 &(ATIM_CR1_DIR_Msk)) == (ATIM_CR1_DIR_Msk))

/**
  * @brief  在 ATIM 运行时设置预分频寄存器(ATIM_PSC)
  * @param  __HANDLE__ ATIM 句柄指针
  * @param  __PRESC__  指定新的预分频值
  * @retval None
  */
#define HAL_ATIM_SET_PRESCALER(__HANDLE__, __PRESC__)    ((__HANDLE__)->Instance->PSC = (__PRESC__))

/**
  * @brief  在 ATIM 运行时设置计数值寄存器(ATIM_CNT)
  * @param  __HANDLE__  ATIM 句柄指针
  * @param  __COUNTER__ 指定新的CNT值
  * @retval None
  */
#define HAL_ATIM_SET_COUNTER(__HANDLE__, __COUNTER__)    ((__HANDLE__)->Instance->CNT = (__COUNTER__))

/**
  * @brief  在 ATIM 运行时获取 ATIM_CNT 值
  * @param  __HANDLE__ ATIM 句柄指针
  * @retval 16 bit 定时器计数器的计数值
  */
#define HAL_ATIM_GET_COUNTER(__HANDLE__)    ((__HANDLE__)->Instance->CNT)

/**
  * @brief  在 ATIM 运行时设置 ATIM_ARR 寄存器值，而无需再次调用任何Init函数
  * @param  __HANDLE__ ATIM 句柄指针
  * @param  __AUTORELOAD__ 指定新的 ATIM_ARR 值
  * @retval None
  */
#define HAL_ATIM_SET_AUTORELOAD(__HANDLE__, __AUTORELOAD__) \
    do {                                                    \
        (__HANDLE__)->Instance->ARR = (__AUTORELOAD__);     \
        (__HANDLE__)->Init.Period = (__AUTORELOAD__);       \
    } while (0)

/**
  * @brief  在 ATIM 运行时获取 ATIM_ARR 寄存器值
  * @param  __HANDLE__ ATIM 句柄指针
  * @retval 16-bit 定时器自动装载寄存器的值
  */
#define HAL_ATIM_GET_AUTORELOAD(__HANDLE__)  ((__HANDLE__)->Instance->ARR)

/**
  * @brief  在 ATIM 运行时设置滤波时钟分频比，而无需再调用任何Init函数
  * @param  __HANDLE__ ATIM 句柄指针
  * @param  __CKD__    指定滤波时钟分频比，该参数可为下列之一:
  *             @arg ATIM_CLOCKDIVISION_DIV1: fDTS = fPCLK
  *             @arg ATIM_CLOCKDIVISION_DIV2: fDTS = fPCLK / 2
  *             @arg ATIM_CLOCKDIVISION_DIV4: fDTS = fPCLK / 4
  * @retval None
  */
#define HAL_ATIM_SET_CLOCKDIVISION(__HANDLE__, __CKD__)     \
    do {                                                    \
        (__HANDLE__)->Instance->CR1 &= (~ATIM_CR1_CKD_Msk); \
        (__HANDLE__)->Instance->CR1 |= (__CKD__);           \
        (__HANDLE__)->Init.ClockDivision = (__CKD__);       \
    } while (0)

/**
  * @brief  在 ATIM 运行时获取滤波时钟分频比
  * @param  __HANDLE__ ATIM 句柄指针
  * @retval 该参数为下列其一:
  *             @arg ATIM_CLOCKDIVISION_DIV1: fDTS = fPCLK
  *             @arg ATIM_CLOCKDIVISION_DIV2: fDTS = fPCLK / 2
  *             @arg ATIM_CLOCKDIVISION_DIV4: fDTS = fPCLK / 4
  */
#define HAL_ATIM_GET_CLOCKDIVISION(__HANDLE__)    ((__HANDLE__)->Instance->CR1 & ATIM_CR1_CKD_Msk)

/**
  * @brief  在 ATIM 运行时获取输入捕获预分频
  * @param  __HANDLE__  ATIM 句柄指针
  * @param  __CHANNEL__ 要配置的 ATIM 通道，该参数可为下列之一:
  *             @arg ATIM_CHANNEL_1: 获取 输入捕获1 的预分频值
  *             @arg ATIM_CHANNEL_2: 获取 输入捕获2 的预分频值
  *             @arg ATIM_CHANNEL_3: 获取 输入捕获3 的预分频值
  *             @arg ATIM_CHANNEL_4: 获取 输入捕获4 的预分频值
  *             @arg ATIM_CHANNEL_5: 获取 输入捕获5 的预分频值
  *             @arg ATIM_CHANNEL_6: 获取 输入捕获6 的预分频值
  * @retval 该返回值为下列之一:
  *             @arg ATIM_ICPSC_DIV1: 每 1 次事件捕获一次
  *             @arg ATIM_ICPSC_DIV2: 每 2 次事件捕获一次
  *             @arg ATIM_ICPSC_DIV4: 每 4 次事件捕获一次
  *             @arg ATIM_ICPSC_DIV8: 每 8 次事件捕获一次
  */
#define HAL_ATIM_GET_ICPRESCALER(__HANDLE__, __CHANNEL__) \
    (((__CHANNEL__) == ATIM_CHANNEL_1) ? (((__HANDLE__)->Instance->CCMR1CAP & ATIM_CCMR1CAP_IC1PSC_Msk) >> 0U) : \
     ((__CHANNEL__) == ATIM_CHANNEL_2) ? (((__HANDLE__)->Instance->CCMR1CAP & ATIM_CCMR1CAP_IC2PSC_Msk) >> 8U) : \
     ((__CHANNEL__) == ATIM_CHANNEL_3) ? (((__HANDLE__)->Instance->CCMR2CAP & ATIM_CCMR2CAP_IC3PSC_Msk) >> 0U) : \
     ((__CHANNEL__) == ATIM_CHANNEL_4) ? (((__HANDLE__)->Instance->CCMR2CAP & ATIM_CCMR2CAP_IC4PSC_Msk) >> 8U) : \
     ((__CHANNEL__) == ATIM_CHANNEL_5) ? (((__HANDLE__)->Instance->CCMR3CAP & ATIM_CCMR3CAP_IC5PSC_Msk) >> 0U) : \
                                         (((__HANDLE__)->Instance->CCMR3CAP & ATIM_CCMR3CAP_IC6PSC_Msk) >> 8U))

/**
  * @brief  在 ATIM 运行时设置 捕获/比较 寄存器值，而不调用另一个ConfigChannel函数
  * @param  __HANDLE__  ATIM 句柄指针.
  * @param  __CHANNEL__ 要配置的 ATIM 通道，该参数可为下列之一:
  *             @arg ATIM_CHANNEL_1: ATIM 通道 1
  *             @arg ATIM_CHANNEL_2: ATIM 通道 2
  *             @arg ATIM_CHANNEL_3: ATIM 通道 3
  *             @arg ATIM_CHANNEL_4: ATIM 通道 4
  *             @arg ATIM_CHANNEL_5: ATIM 通道 5
  *             @arg ATIM_CHANNEL_6: ATIM 通道 6
  * @param  __COMPARE__ 指定新的 捕获/比较 寄存器值
  * @retval None
  */
#define HAL_ATIM_SET_COMPARE(__HANDLE__, __CHANNEL__, __COMPARE__) \
    (((__CHANNEL__) == ATIM_CHANNEL_1) ? ((__HANDLE__)->Instance->CCR1 = (__COMPARE__)) : \
     ((__CHANNEL__) == ATIM_CHANNEL_2) ? ((__HANDLE__)->Instance->CCR2 = (__COMPARE__)) : \
     ((__CHANNEL__) == ATIM_CHANNEL_3) ? ((__HANDLE__)->Instance->CCR3 = (__COMPARE__)) : \
     ((__CHANNEL__) == ATIM_CHANNEL_4) ? ((__HANDLE__)->Instance->CCR4 = (__COMPARE__)) : \
     ((__CHANNEL__) == ATIM_CHANNEL_5) ? ((__HANDLE__)->Instance->CCR5 = (__COMPARE__)) : \
                                         ((__HANDLE__)->Instance->CCR6 = (__COMPARE__)))

/**
  * @brief  在 ATIM 运行时获取 捕获/比较 寄存器值
  * @param  __HANDLE__ ATIM 句柄指针
  * @param  __CHANNEL__ 与 捕获/比较 寄存器相关联的ATIM通道，该参数可为下列之一:
  *            @arg ATIM_CHANNEL_1: 获取 捕获/比较1 (CCR1) 寄存器值
  *            @arg ATIM_CHANNEL_2: 获取 捕获/比较2 (CCR2) 寄存器值
  *            @arg ATIM_CHANNEL_3: 获取 捕获/比较3 (CCR3) 寄存器值
  *            @arg ATIM_CHANNEL_4: 获取 捕获/比较4 (CCR4) 寄存器值
  *            @arg ATIM_CHANNEL_5: 获取 捕获/比较5 (CCR5) 寄存器值
  *            @arg ATIM_CHANNEL_6: 获取 捕获/比较6 (CCR6) 寄存器值
  * @retval 16-bit 的 捕获/比较 寄存器值 (CCRx)
  */
#define HAL_ATIM_GET_COMPARE(__HANDLE__, __CHANNEL__) \
    (((__CHANNEL__) == ATIM_CHANNEL_1) ? ((__HANDLE__)->Instance->CCR1) : \
     ((__CHANNEL__) == ATIM_CHANNEL_2) ? ((__HANDLE__)->Instance->CCR2) : \
     ((__CHANNEL__) == ATIM_CHANNEL_3) ? ((__HANDLE__)->Instance->CCR3) : \
     ((__CHANNEL__) == ATIM_CHANNEL_4) ? ((__HANDLE__)->Instance->CCR4) : \
     ((__CHANNEL__) == ATIM_CHANNEL_5) ? ((__HANDLE__)->Instance->CCR5) : \
                                         ((__HANDLE__)->Instance->CCR6))

/**
  * @brief  使能 ATIM 输出比较 CCRx预装载
  * @param  __HANDLE__  ATIM 句柄指针
  * @param  __CHANNEL__ 要配置的 ATIM 通道，该参数可为下列之一:
  *             @arg ATIM_CHANNEL_1: ATIM 通道 1
  *             @arg ATIM_CHANNEL_2: ATIM 通道 2
  *             @arg ATIM_CHANNEL_3: ATIM 通道 3
  *             @arg ATIM_CHANNEL_4: ATIM 通道 4
  *             @arg ATIM_CHANNEL_5: ATIM 通道 5
  *             @arg ATIM_CHANNEL_6: ATIM 通道 6
  * @retval None
  */
#define HAL_ATIM_ENABLE_OCxPRELOAD(__HANDLE__, __CHANNEL__) \
    (((__CHANNEL__) == ATIM_CHANNEL_1) ? ((__HANDLE__)->Instance->CCMR1CMP |= ATIM_CCMR1CMP_OC1PE_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_2) ? ((__HANDLE__)->Instance->CCMR1CMP |= ATIM_CCMR1CMP_OC2PE_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_3) ? ((__HANDLE__)->Instance->CCMR2CMP |= ATIM_CCMR2CMP_OC3PE_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_4) ? ((__HANDLE__)->Instance->CCMR2CMP |= ATIM_CCMR2CMP_OC4PE_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_5) ? ((__HANDLE__)->Instance->CCMR3CMP |= ATIM_CCMR3CMP_OC5PE_Msk) : \
                                         ((__HANDLE__)->Instance->CCMR3CMP |= ATIM_CCMR3CMP_OC6PE_Msk))

/**
  * @brief  禁止 ATIM 输出比较 CCRx预装载
  * @param  __HANDLE__  ATIM 句柄指针
  * @param  __CHANNEL__ 要配置的 ATIM 通道，该参数可为下列之一:
  *             @arg ATIM_CHANNEL_1: ATIM 通道 1
  *             @arg ATIM_CHANNEL_2: ATIM 通道 2
  *             @arg ATIM_CHANNEL_3: ATIM 通道 3
  *             @arg ATIM_CHANNEL_4: ATIM 通道 4
  *             @arg ATIM_CHANNEL_5: ATIM 通道 5
  *             @arg ATIM_CHANNEL_6: ATIM 通道 6
  * @retval None
  */
#define HAL_ATIM_DISABLE_OCxPRELOAD(__HANDLE__, __CHANNEL__) \
    (((__CHANNEL__) == ATIM_CHANNEL_1) ? ((__HANDLE__)->Instance->CCMR1CMP &= ~ATIM_CCMR1CMP_OC1PE_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_2) ? ((__HANDLE__)->Instance->CCMR1CMP &= ~ATIM_CCMR1CMP_OC2PE_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_3) ? ((__HANDLE__)->Instance->CCMR2CMP &= ~ATIM_CCMR2CMP_OC3PE_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_4) ? ((__HANDLE__)->Instance->CCMR2CMP &= ~ATIM_CCMR2CMP_OC4PE_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_5) ? ((__HANDLE__)->Instance->CCMR3CMP &= ~ATIM_CCMR3CMP_OC5PE_Msk) : \
                                         ((__HANDLE__)->Instance->CCMR3CMP &= ~ATIM_CCMR3CMP_OC6PE_Msk))

/**
  * @brief  为指定通道启用快速模式(OCxFE)
  * @param  __HANDLE__  ATIM 句柄指针
  * @param  __CHANNEL__ 要配置的 ATIM 通道，该参数可为下列之一:
  *             @arg ATIM_CHANNEL_1: ATIM 通道 1
  *             @arg ATIM_CHANNEL_2: ATIM 通道 2
  *             @arg ATIM_CHANNEL_3: ATIM 通道 3
  *             @arg ATIM_CHANNEL_4: ATIM 通道 4
  *             @arg ATIM_CHANNEL_5: ATIM 通道 5
  *             @arg ATIM_CHANNEL_6: ATIM 通道 6
  * @note   当启用快速模式时，触发器输入上的活动边缘就像CCx输出上的比较匹配一样。
  *             采样触发输入和激活CCx输出的延迟减少到3个时钟周期。
  * @note   仅适用于单脉冲模式
  * @retval None
  */
#define HAL_ATIM_ENABLE_OCxFAST(__HANDLE__, __CHANNEL__) \
    (((__CHANNEL__) == ATIM_CHANNEL_1) ? ((__HANDLE__)->Instance->CCMR1CMP |= ATIM_CCMR1CMP_OC1FE_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_2) ? ((__HANDLE__)->Instance->CCMR1CMP |= ATIM_CCMR1CMP_OC2FE_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_3) ? ((__HANDLE__)->Instance->CCMR2CMP |= ATIM_CCMR2CMP_OC3FE_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_4) ? ((__HANDLE__)->Instance->CCMR2CMP |= ATIM_CCMR2CMP_OC4FE_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_5) ? ((__HANDLE__)->Instance->CCMR3CMP |= ATIM_CCMR3CMP_OC5FE_Msk) : \
                                         ((__HANDLE__)->Instance->CCMR3CMP |= ATIM_CCMR3CMP_OC6FE_Msk))

/**
  * @brief  为指定通道禁用快速模式(OCxFE)
  * @param  __HANDLE__  ATIM 句柄指针
  * @param  __CHANNEL__ 要配置的 ATIM 通道，该参数可为下列之一:
  *             @arg ATIM_CHANNEL_1: ATIM 通道 1
  *             @arg ATIM_CHANNEL_2: ATIM 通道 2
  *             @arg ATIM_CHANNEL_3: ATIM 通道 3
  *             @arg ATIM_CHANNEL_4: ATIM 通道 4
  *             @arg ATIM_CHANNEL_5: ATIM 通道 5
  *             @arg ATIM_CHANNEL_6: ATIM 通道 6
  * @note   当禁用快速模式时，CCx输出行为通常取决于计数器和CCRx值，
  *             即使触发器是打开的。当一个活动边缘发生在触发器输入上时，
  *             激活CCx输出的最小延迟是5个时钟周期。
  * @retval None
  */
#define HAL_ATIM_DISABLE_OCxFAST(__HANDLE__, __CHANNEL__) \
    (((__CHANNEL__) == ATIM_CHANNEL_1) ? ((__HANDLE__)->Instance->CCMR1CMP &= ~ATIM_CCMR1CMP_OC1FE_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_2) ? ((__HANDLE__)->Instance->CCMR1CMP &= ~ATIM_CCMR1CMP_OC2FE_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_3) ? ((__HANDLE__)->Instance->CCMR2CMP &= ~ATIM_CCMR2CMP_OC3FE_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_4) ? ((__HANDLE__)->Instance->CCMR2CMP &= ~ATIM_CCMR2CMP_OC4FE_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_5) ? ((__HANDLE__)->Instance->CCMR3CMP &= ~ATIM_CCMR3CMP_OC5FE_Msk) : \
                                         ((__HANDLE__)->Instance->CCMR3CMP &= ~ATIM_CCMR3CMP_OC6FE_Msk))

/**
  * @brief  置位 ATIMx_CR1 寄存器的更新请求源(URS)位。
  * @param  __HANDLE__ ATIM 句柄指针
  * @note   使能时，只有计数器上溢/下溢会生成更新中断或DMA请求(如果存在并且启用)
  * @retval None
  */
#define HAL_ATIM_URS_ENABLE(__HANDLE__)    ((__HANDLE__)->Instance->CR1 |= ATIM_CR1_URS_Msk)

/**
  * @brief  重置 ATIMx_CR1 寄存器的更新请求源(URS)位。
  * @param  __HANDLE__ ATIM 句柄指针
  * @note   当 ATIMx_CR1 寄存器的URS位被重置时，下列任何事件都会产生一个更新中
  *         断或DMA请求(如果存在并且启用):
  *             _ 计数器上溢/下溢
  *             _ 将UG位置1
  *             _ 通过从模式控制器生成的更新事件
  * @retval None
  */
#define HAL_ATIM_URS_DISABLE(__HANDLE__)    ((__HANDLE__)->Instance->CR1 &=~ ATIM_CR1_URS_Msk)

/**
  * @brief  辅助宏: 计算分频器的值，以达到所需的计数器时钟频率
  * @note   ex: @ref HAL_ATIM_CALC_PSC(80000000, 1000000);
  * @param  __TIMCLK__ 定时器输入时钟频率 (Hz)
  * @param  __CNTCLK__ 计数器时钟频率 (Hz)
  * @retval Prescaler value  (between Min_Data=0 and Max_Data=65535)
  */
#define HAL_ATIM_CALC_PSC(__TIMCLK__, __CNTCLK__) \
    ((__TIMCLK__) >= (__CNTCLK__)) ? (uint32_t)((__TIMCLK__)/(__CNTCLK__) - 1U) : 0U

/**
  * @brief  辅助宏: 计算自动重载值以达到所需的输出信号频率
  * @note   ex: @ref HAL_ATIM_CALC_PERIOD(1000000, 0, 10000);
  * @param  __TIMCLK__ 定时器输入时钟频率 (Hz)
  * @param  __PSC__    定时器分频值
  * @param  __FREQ__   输出信号频率 (Hz)
  * @retval  Auto-reload value  (between Min_Data=0 and Max_Data=65535)
  */
#define HAL_ATIM_CALC_PERIOD(__TIMCLK__, __PSC__, __FREQ__) \
    (((__TIMCLK__)/((__PSC__) + 1U)) >= (__FREQ__)) ? ((__TIMCLK__)/((__FREQ__) * ((__PSC__) + 1U)) - 1U) : 0U

/**
  * @brief  辅助宏: 计算实现定时器输出所需的 active/inactive 延迟的比较值
  * @note   ex: @ref HAL_ATIM_CALC_PULSE(1000000, 0, 10);
  * @param  __TIMCLK__ 定时器输入时钟频率 (Hz)
  * @param  __PSC__    定时器分频值
  * @param  __DELAY__  定时器输出所需的 active/inactive 延迟 (us)
  * @retval Compare value  (between Min_Data=0 and Max_Data=65535)
  */
#define HAL_ATIM_CALC_PULSE(__TIMCLK__, __PSC__, __DELAY__) \
    ((uint32_t)(((uint64_t)(__TIMCLK__) * (uint64_t)(__DELAY__)) \
              / ((uint64_t)1000000U * (uint64_t)((__PSC__) + 1U))))

/**
  * @brief  辅助宏: 计算自动重载值以达到所需的脉冲持续时间(当定时器以单脉冲模式工作时)。
  * @note   ex: @ref HAL_ATIM_CALC_PERIOD_BY_DELAY(1000000, 0, 10, 20);
  * @param  __TIMCLK__ 定时器输入时钟频率 (Hz)
  * @param  __PSC__    定时器分频值
  * @param  __DELAY__  定时器输出所需的 active/inactive 延迟 (us)
  * @param  __PULSE__  脉冲持续时间 (us)
  * @retval Auto-reload value  (between Min_Data=0 and Max_Data=65535)
  */
#define HAL_ATIM_CALC_PERIOD_BY_DELAY(__TIMCLK__, __PSC__, __DELAY__, __PULSE__) \
    ((uint32_t)(HAL_ATIM_CALC_PULSE((__TIMCLK__), (__PSC__), (__PULSE__)) \
              + HAL_ATIM_CALC_PULSE((__TIMCLK__), (__PSC__), (__DELAY__))))

#define ATIM_SET_ICPRESCALERVALUE(__HANDLE__, __CHANNEL__, __ICPSC__) \
    (((__CHANNEL__) == ATIM_CHANNEL_1) ? ((__HANDLE__)->Instance->CCMR1CAP |= ((__ICPSC__) << 0U)) : \
     ((__CHANNEL__) == ATIM_CHANNEL_2) ? ((__HANDLE__)->Instance->CCMR1CAP |= ((__ICPSC__) << 8U)) : \
     ((__CHANNEL__) == ATIM_CHANNEL_3) ? ((__HANDLE__)->Instance->CCMR2CAP |= ((__ICPSC__) << 0U)) : \
     ((__CHANNEL__) == ATIM_CHANNEL_4) ? ((__HANDLE__)->Instance->CCMR2CAP |= ((__ICPSC__) << 8U)) : \
     ((__CHANNEL__) == ATIM_CHANNEL_5) ? ((__HANDLE__)->Instance->CCMR3CAP |= ((__ICPSC__) << 0U)) : \
                                         ((__HANDLE__)->Instance->CCMR3CAP |= ((__ICPSC__) << 8U)))

#define ATIM_RESET_ICPRESCALERVALUE(__HANDLE__, __CHANNEL__) \
    (((__CHANNEL__) == ATIM_CHANNEL_1) ? ((__HANDLE__)->Instance->CCMR1CAP &= ~ATIM_CCMR1CAP_IC1PSC_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_2) ? ((__HANDLE__)->Instance->CCMR1CAP &= ~ATIM_CCMR1CAP_IC2PSC_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_3) ? ((__HANDLE__)->Instance->CCMR2CAP &= ~ATIM_CCMR2CAP_IC3PSC_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_4) ? ((__HANDLE__)->Instance->CCMR2CAP &= ~ATIM_CCMR2CAP_IC4PSC_Msk) : \
     ((__CHANNEL__) == ATIM_CHANNEL_5) ? ((__HANDLE__)->Instance->CCMR3CAP &= ~ATIM_CCMR3CAP_IC5PSC_Msk) : \
                                         ((__HANDLE__)->Instance->CCMR3CAP &= ~ATIM_CCMR3CAP_IC6PSC_Msk))

#define ATIM_SET_CAPTUREPOLARITY(__HANDLE__, __CHANNEL__, __POLARITY__) \
    (((__CHANNEL__) == ATIM_CHANNEL_1) ? ((__HANDLE__)->Instance->CCER |= ((__POLARITY__) << 0U))  : \
     ((__CHANNEL__) == ATIM_CHANNEL_2) ? ((__HANDLE__)->Instance->CCER |= ((__POLARITY__) << 4U))  : \
     ((__CHANNEL__) == ATIM_CHANNEL_3) ? ((__HANDLE__)->Instance->CCER |= ((__POLARITY__) << 8U))  : \
     ((__CHANNEL__) == ATIM_CHANNEL_4) ? ((__HANDLE__)->Instance->CCER |= ((__POLARITY__) << 12U)) : \
     ((__CHANNEL__) == ATIM_CHANNEL_5) ? ((__HANDLE__)->Instance->CCER |= ((__POLARITY__) << 16U)) : \
                                         ((__HANDLE__)->Instance->CCER |= ((__POLARITY__) << 20U)))

#define ATIM_RESET_CAPTUREPOLARITY(__HANDLE__, __CHANNEL__) \
    (((__CHANNEL__) == ATIM_CHANNEL_1) ? ((__HANDLE__)->Instance->CCER &= ~(ATIM_CCER_CC1P_Msk | ATIM_CCER_CC1NP_Msk)) : \
     ((__CHANNEL__) == ATIM_CHANNEL_2) ? ((__HANDLE__)->Instance->CCER &= ~(ATIM_CCER_CC2P_Msk | ATIM_CCER_CC2NP_Msk)) : \
     ((__CHANNEL__) == ATIM_CHANNEL_3) ? ((__HANDLE__)->Instance->CCER &= ~(ATIM_CCER_CC3P_Msk | ATIM_CCER_CC3NP_Msk)) : \
     ((__CHANNEL__) == ATIM_CHANNEL_4) ? ((__HANDLE__)->Instance->CCER &= ~(ATIM_CCER_CC4P_Msk | ATIM_CCER_CC4NP_Msk)) : \
     ((__CHANNEL__) == ATIM_CHANNEL_5) ? ((__HANDLE__)->Instance->CCER &= ~(ATIM_CCER_CC5P_Msk | ATIM_CCER_CC5NP_Msk)) : \
                                         ((__HANDLE__)->Instance->CCER &= ~(ATIM_CCER_CC6P_Msk | ATIM_CCER_CC6NP_Msk)))

/**
  * @brief  在 ATIM 运行时设置输入捕获预分频，而不调用HAL_ATIM_IC_ConfigChannel()函数
  * @param  __HANDLE__  ATIM 句柄指针
  * @param  __CHANNEL__ 要配置的 ATIM 通道，该参数可为下列之一:
  *             @arg ATIM_CHANNEL_1: ATIM 通道 1
  *             @arg ATIM_CHANNEL_2: ATIM 通道 2
  *             @arg ATIM_CHANNEL_3: ATIM 通道 3
  *             @arg ATIM_CHANNEL_4: ATIM 通道 4
  *             @arg ATIM_CHANNEL_5: ATIM 通道 5
  *             @arg ATIM_CHANNEL_6: ATIM 通道 6
  * @param  __ICPSC__ 指定输入捕获的预分频新值，该参数可为下列之一:
  *             @arg ATIM_ICPSC_DIV1: 每 1 次事件捕获一次
  *             @arg ATIM_ICPSC_DIV2: 每 2 次事件捕获一次
  *             @arg ATIM_ICPSC_DIV4: 每 4 次事件捕获一次
  *             @arg ATIM_ICPSC_DIV8: 每 8 次事件捕获一次
  * @retval None
  */
#define HAL_ATIM_SET_ICPRESCALER(__HANDLE__, __CHANNEL__, __ICPSC__)           \
    do                                                                         \
    {                                                                          \
        ATIM_RESET_ICPRESCALERVALUE((__HANDLE__), (__CHANNEL__));              \
        ATIM_SET_ICPRESCALERVALUE((__HANDLE__), (__CHANNEL__), (__ICPSC__));   \
    } while (0)

/**
  * @brief  在 ATIM 运行时设置 Capturex 输入极性。
  * @param  __HANDLE__  ATIM 句柄指针
  * @param  __CHANNEL__ 要配置的 ATIM 通道，该参数可为下列之一:
  *             @arg ATIM_CHANNEL_1: ATIM 通道 1
  *             @arg ATIM_CHANNEL_2: ATIM 通道 2
  *             @arg ATIM_CHANNEL_3: ATIM 通道 3
  *             @arg ATIM_CHANNEL_4: ATIM 通道 4
  *             @arg ATIM_CHANNEL_5: ATIM 通道 5
  *             @arg ATIM_CHANNEL_6: ATIM 通道 6
  * @param  __POLARITY__ TIx源的极性
  *             @arg ATIM_INPUTCHANNELPOLARITY_RISING: 上升沿
  *             @arg ATIM_INPUTCHANNELPOLARITY_FALLING: 下降沿
  *             @arg ATIM_INPUTCHANNELPOLARITY_BOTHEDGE: 双边沿
  * @retval None
  */
#define HAL_ATIM_SET_CAPTUREPOLARITY(__HANDLE__, __CHANNEL__, __POLARITY__)    \
    do                                                                         \
    {                                                                          \
        ATIM_RESET_CAPTUREPOLARITY((__HANDLE__), (__CHANNEL__));               \
        ATIM_SET_CAPTUREPOLARITY((__HANDLE__), (__CHANNEL__), (__POLARITY__)); \
    } while (0)


/******************************************************************************/
/* Global function prototypes ('extern', definition in C source)              */
/******************************************************************************/
/* Time Base functions */
HAL_StatusTypeDef HAL_ATIM_Base_Init(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_Base_DeInit(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_Base_Start(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_Base_Stop(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_Base_Start_IT(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_Base_Stop_IT(ATIM_HandleTypeDef *htim);

/* Timer Output Compare functions */
HAL_StatusTypeDef HAL_ATIM_OC_Init(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_OC_DeInit(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_OC_Start(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_OC_Stop(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_OC_Start_IT(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_OC_Stop_IT(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_OCN_Start(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_OCN_Stop(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_OCN_Start_IT(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_OCN_Stop_IT(ATIM_HandleTypeDef *htim, uint32_t Channel);

/* Timer PWM functions */
HAL_StatusTypeDef HAL_ATIM_PWM_Init(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_PWM_DeInit(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_PWM_Start(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_PWM_Stop(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_PWM_Start_IT(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_PWM_Stop_IT(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_PWMN_Start(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_PWMN_Stop(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_PWMN_Start_IT(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_PWMN_Stop_IT(ATIM_HandleTypeDef *htim, uint32_t Channel);

/* Timer One Pulse functions */
HAL_StatusTypeDef HAL_ATIM_OnePulse_Init(ATIM_HandleTypeDef *htim, uint32_t OnePulseMode);
HAL_StatusTypeDef HAL_ATIM_OnePulse_DeInit(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_OnePulse_Start(ATIM_HandleTypeDef *htim, uint32_t OutputChannel);
HAL_StatusTypeDef HAL_ATIM_OnePulse_Stop(ATIM_HandleTypeDef *htim, uint32_t OutputChannel);
HAL_StatusTypeDef HAL_ATIM_OnePulse_Start_IT(ATIM_HandleTypeDef *htim, uint32_t OutputChannel);
HAL_StatusTypeDef HAL_ATIM_OnePulse_Stop_IT(ATIM_HandleTypeDef *htim, uint32_t OutputChannel);
HAL_StatusTypeDef HAL_ATIM_OnePulseN_Start(ATIM_HandleTypeDef *htim, uint32_t OutputChannel);
HAL_StatusTypeDef HAL_ATIM_OnePulseN_Stop(ATIM_HandleTypeDef *htim, uint32_t OutputChannel);
HAL_StatusTypeDef HAL_ATIM_OnePulseN_Start_IT(ATIM_HandleTypeDef *htim, uint32_t OutputChannel);
HAL_StatusTypeDef HAL_ATIM_OnePulseN_Stop_IT(ATIM_HandleTypeDef *htim, uint32_t OutputChannel);

/* Timer Input Capture functions */
HAL_StatusTypeDef HAL_ATIM_IC_Init(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_IC_DeInit(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_IC_Start(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_IC_Stop(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_IC_Start_IT(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_IC_Stop_IT(ATIM_HandleTypeDef *htim, uint32_t Channel);

/* Timer Encoder functions */
HAL_StatusTypeDef HAL_ATIM_Encoder_Init(ATIM_HandleTypeDef *htim,  ATIM_Encoder_InitTypeDef *sConfig);
HAL_StatusTypeDef HAL_ATIM_Encoder_DeInit(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_Encoder_Start(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_Encoder_Stop(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_Encoder_Start_IT(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_Encoder_Stop_IT(ATIM_HandleTypeDef *htim, uint32_t Channel);

/*  Timer Hall Sensor functions */
HAL_StatusTypeDef HAL_ATIM_HallSensor_Init(ATIM_HandleTypeDef *htim, ATIM_HallSensor_InitTypeDef *sConfig);
HAL_StatusTypeDef HAL_ATIM_HallSensor_DeInit(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_HallSensor_Start(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_HallSensor_Stop(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_HallSensor_Start_IT(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_HallSensor_Stop_IT(ATIM_HandleTypeDef *htim);

/* Control functions */
HAL_StatusTypeDef HAL_ATIM_OC_ConfigChannel(ATIM_HandleTypeDef *htim, ATIM_OC_InitTypeDef *sConfig, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_PWM_ConfigChannel(ATIM_HandleTypeDef *htim, ATIM_OC_InitTypeDef *sConfig, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_IC_ConfigChannel(ATIM_HandleTypeDef *htim, ATIM_IC_InitTypeDef *sConfig, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_OnePulse_ConfigChannel(ATIM_HandleTypeDef *htim, ATIM_OnePulse_InitTypeDef *sConfig,
                                                  uint32_t OutputChannel, uint32_t InputChannel);
HAL_StatusTypeDef HAL_ATIM_ConfigOCrefClear(ATIM_HandleTypeDef *htim, ATIM_ClearInputConfigTypeDef *sClearInputConfig,
                                            uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_ConfigClockSource(ATIM_HandleTypeDef *htim, ATIM_ClockConfigTypeDef *sClockSourceConfig);
HAL_StatusTypeDef HAL_ATIM_ConfigTI1Input(ATIM_HandleTypeDef *htim, uint32_t TI1_Selection);
HAL_StatusTypeDef HAL_ATIM_SlaveConfigSynchro(ATIM_HandleTypeDef *htim, ATIM_SlaveConfigTypeDef *sSlaveConfig);
HAL_StatusTypeDef HAL_ATIM_SlaveConfigSynchro_IT(ATIM_HandleTypeDef *htim, ATIM_SlaveConfigTypeDef *sSlaveConfig);
HAL_StatusTypeDef HAL_ATIM_GenerateEvent(ATIM_HandleTypeDef *htim, uint32_t EventSource);

/* Channel Captured value or state functions */
uint32_t HAL_ATIM_ReadCapturedValue(ATIM_HandleTypeDef *htim, uint32_t Channel);
HAL_ATIM_ActiveChannel HAL_ATIM_GetActiveChannel(ATIM_HandleTypeDef *htim);

/* Base functions */
void ATIM_Base_SetConfig(ATIM_TypeDef *TIMx, ATIM_Base_InitTypeDef *Structure);
void ATIM_ETR_SetConfig(ATIM_TypeDef *TIMx, uint32_t TIM_ExtTRGPrescaler, uint32_t TIM_ExtTRGPolarity, uint32_t ExtTRGFilter);
void ATIM_CCxChannelCmd(ATIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelState);
void ATIM_CCxNChannelCmd(ATIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelNState);

/* Extended Control functions */
HAL_StatusTypeDef HAL_ATIM_ConfigCommutEvent(ATIM_HandleTypeDef *htim, uint32_t  InputTrigger, uint32_t  CommutationSource);
HAL_StatusTypeDef HAL_ATIM_ConfigCommutEvent_IT(ATIM_HandleTypeDef *htim, uint32_t  InputTrigger, uint32_t  CommutationSource);
HAL_StatusTypeDef HAL_ATIM_MasterConfigSynchronization(ATIM_HandleTypeDef *htim, ATIM_MasterConfigTypeDef *sMasterConfig);
HAL_StatusTypeDef HAL_ATIM_ConfigBreakDeadTime(ATIM_HandleTypeDef *htim, ATIM_BreakDeadTimeConfigTypeDef *sBreakDeadTimeConfig);
HAL_StatusTypeDef HAL_ATIM_ConfigBreakInput(ATIM_HandleTypeDef *htim, uint32_t BreakInput, ATIM_BreakInputConfigTypeDef *sBreakInputConfig);
HAL_StatusTypeDef HAL_ATIM_GroupChannel5(ATIM_HandleTypeDef *htim, uint32_t Channels);
HAL_StatusTypeDef HAL_ATIM_GroupChannel6(ATIM_HandleTypeDef *htim, uint32_t Channels);
HAL_StatusTypeDef HAL_ATIM_ETRSelection(ATIM_HandleTypeDef *htim, uint32_t ETRSelection);
HAL_StatusTypeDef HAL_ATIM_RemapConfig(ATIM_HandleTypeDef *htim, uint32_t Remap);
HAL_StatusTypeDef HAL_ATIM_TISelection(ATIM_HandleTypeDef *htim, uint32_t TISelection, uint32_t Channel);
HAL_StatusTypeDef HAL_ATIM_ConfigSlaveModePreload(ATIM_HandleTypeDef *htim, uint32_t Source);
HAL_StatusTypeDef HAL_ATIM_EnableSlaveModePreload(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_DisableSlaveModePreload(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_EnableDeadTimePreload(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_DisableDeadTimePreload(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_ConfigDeadTime(ATIM_HandleTypeDef *htim, uint32_t Deadtime);
HAL_StatusTypeDef HAL_ATIM_ConfigAsymmetricalDeadTime(ATIM_HandleTypeDef *htim, uint32_t FallingDeadtime);
HAL_StatusTypeDef HAL_ATIM_EnableAsymmetricalDeadTime(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_DisableAsymmetricalDeadTime(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_ConfigEncoderIndex(ATIM_HandleTypeDef *htim, ATIM_EncoderIndexConfigTypeDef *sEncoderIndexConfig);
HAL_StatusTypeDef HAL_ATIM_EnableEncoderIndex(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_DisableEncoderIndex(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_EnableEncoderFirstIndex(ATIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_ATIM_DisableEncoderFirstIndex(ATIM_HandleTypeDef *htim);

/* Interrupt Handler functions */
void HAL_ATIM_IRQHandler(ATIM_HandleTypeDef *htim);

/* Callback in non blocking modes (Interrupt) */
void HAL_ATIM_PeriodElapsedCallback(ATIM_HandleTypeDef *htim);
void HAL_ATIM_OC_DelayElapsedCallback(ATIM_HandleTypeDef *htim);
void HAL_ATIM_IC_CaptureCallback(ATIM_HandleTypeDef *htim);
void HAL_ATIM_PWM_PulseFinishedCallback(ATIM_HandleTypeDef *htim);
void HAL_ATIM_TriggerCallback(ATIM_HandleTypeDef *htim);
void HAL_ATIM_ErrorCallback(ATIM_HandleTypeDef *htim);

/* Extended Callback */
void HAL_ATIM_CommutCallback(ATIM_HandleTypeDef *htim);
void HAL_ATIM_BreakCallback(ATIM_HandleTypeDef *htim);
void HAL_ATIM_Break2Callback(ATIM_HandleTypeDef *htim);
void HAL_ATIM_EncoderIndexCallback(ATIM_HandleTypeDef *htim);
void HAL_ATIM_DirectionChangeCallback(ATIM_HandleTypeDef *htim);
void HAL_ATIM_IndexErrorCallback(ATIM_HandleTypeDef *htim);
void HAL_ATIM_TransitionErrorCallback(ATIM_HandleTypeDef *htim);


#ifdef __cplusplus
}
#endif

#endif /* __SYM32L010_HAL_ATIM_H */

/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
